如何使工作区中的零件可见并开始移动?

How do I make a part, which is in Workspace, visible and start moving?

我正在尝试使用的按钮位于 StarterGUI 中。不知道有没有帮助。相当新的脚本,但是看到另一个 post 有类似的东西并使用了那个代码。这是我在 LocalScript 中的代码:

local MarketplaceService = game:GetService("MarketplaceService")
local player = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.PurchaseEvent
local productID = 1218445531

  
script.Parent.MouseButton1Click:Connect(function()
    
    PurchaseEvent:FireServer(productID)
end)

if MarketplaceService:PlayerOwnsAsset(1218445531) then
    --Here is where the thing is supposed to happen
end

首先,您需要获得对零件的引用,一种方法是使用类似 local part = workspace:WaitForChild("PartName")

的内容

为了使其可见,您需要将透明度设置为 0。如果您不希望玩家穿过该部分,您可能还需要使用 CanColide 打开碰撞。还建议固定零件以防止其掉落。

part.Transparency = 0; -- makes it visible
part.CanColide = true; -- makes sure other objects cant go through this
part.Anchored = true; -- prevents the object from falling

有很多方法可以使零件移动。如果该部分应该从一个地方到另一个地方,并且只有一个目的地,则 Tween 可能会有用(参见 TweenService)。

另一种方法是使用 Heartbeat 循环,每帧更改位置(请参阅 RunService.Heartbeat)。例如,

local keepMoving = true; -- change this to false to stop moving
local RunService = game:GetService("RunService");
local direction = Vector3.new(0, 1, 0); -- change this to anything you want
while (keepMoving) do
    local timePassed = RunService.Heartbeat:Wait(); -- wait one frame, roughly 0.016 of a second.
    part.Position += direction * timePassed; -- move the part in the direction 
end

这将使 part 每秒上升 1 个螺柱。