【问题标题】:How do I make a part, which is in Workspace, visible and start moving?如何使 Workspace 中的零件可见并开始移动?
【发布时间】:2021-12-20 19:08:56
【问题描述】:

我正在尝试使用的按钮位于 StarterGUI 中。不知道有没有帮助。脚本编写相当新,但是看到另一篇类似的帖子并使用了该代码。这是我在 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

【问题讨论】:

    标签: lua scripting roblox


    【解决方案1】:

    首先,您需要获得对零件的引用,一种方法是使用 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 个螺柱。

    【讨论】:

    • 将方向更改为 Vector3.new(0, 0, 5) 将使其每秒 5 个螺柱,并且应该与每秒向 Z 轴添加 5 个相同。答案中的示例与每秒向 Y 轴加 1 相同,这会将其向上移动。
    • 现在可以了!这对我很有帮助!
    • 既然它有效,你能把它标记为接受的答案吗?
    猜你喜欢
    • 2020-09-05
    • 1970-01-01
    • 2010-12-04
    • 2023-02-01
    • 2017-08-06
    • 1970-01-01
    • 2018-05-02
    • 2021-06-20
    • 1970-01-01
    相关资源
    最近更新 更多