Need help on ROBLOX script (Lua)

382 views Asked by At

I`m making a script for my UFO in ROBLOX where whenever the UFO passes overhead it plays an audio. I made a script that goes as follows

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

Any corrections would be a real help!

Thanks!

1

There are 1 answers

0
Sean Colin Roach On

Everything aside, at the root there are a lot of syntax errors as well as the error of implementation, all in all the code block you've given us won't do what you wanted it to.

However, here's a basic idea of what the solution is:

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)

Essentially, here's the rundown: We're using coroutines so this while loop doesn't yield this thread for all eternity. There are some more complex avenues using bindable events and maybe some server-client handshake where, when fired from the LocalPlayer, it calls an event in the server- but I digress. This should be perfect for your needs without confusing you too much.