ROBLOX 脚本(Lua)需要帮助

我正在为 ROBLOX 中的我的 UFO 制作脚本,每当 UFO 经过头顶时它就会播放一个音频。我编写的脚本如下所示

while true do
    if script.Parent.Parent.Velocity.Magnitude>10 then
    if local h = hit.Parent:FindFirstChild("Humanoid")
        then script.parent:play()
        wait(5)
    else
        wait()
   end
   wait()
end

任何修改意见都将是真正的帮助!

谢谢!

点赞
用户7452073
用户7452073

不管其他的事情,从根本上说,你给我们的代码块存在许多语法错误以及实现上的错误,总之它不会实现你想要的功能。

然而,这里是解决方案的基本思路:

local newThread = coroutine.create(function()
    while true do
        for i, v in game.Players:GetPlayers()
            local playerListener = workspace[v.Name]["Head"]
            local ufoListener = workspace.Ufo:FindFirstChild('Listener')
            local magnitude = (playerListener.Position - ufoListener.Position).magnitude
            if (magnitude > 10) then
                script.Parent:play()
            else
                wait(0.5)
            end
        end
    end
end)

coroutine.resume(newThread)

基本上,这是概述:我们使用协程,以便这个 while 循环不会永远占用运行线程。还有一些更复杂的方式,如使用可绑定事件和一些服务器-客户端握手,当从本地玩家触发时,它会调用服务器中的事件-但我走了神。这应该完美地满足您的需求,而不会让您感到过于困惑。

2017-01-22 08:21:16