Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is the "wait()" method not allowing my script to run?

Asked by 7 years ago
Edited 7 years ago

I am making a game that once a button is clicked a serious of lanterns are released around the game. My code runs fine, except for when i add the "wait(40)" you see at line 15.

When the wait is there only ONE lantern is released.

I tested my code without the wait and it runs perfectly.

Here is my code:


local light = script.Parent.LIGHT script.Parent.ClickDetector.MouseClick:connect(function() script.Parent.Transparency = 1 local la = game.Workspace.Lanterns local lag = la:GetChildren() for _,v in pairs (lag) do if v.Name == "LanternAtt" then local attA = v:FindFirstChild("AttachmentA") if attA then local at = attA:FindFirstChild("Attachment") if at then light:Play() at:Destroy() wait(40) game.Workspace.Atmosphere.ParticleEmitter.Enabled = false end end end end wait(100) for _,v in pairs (lag) do v:Destroy() end end)

If anyone could help that would be amazing! Thanks,

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

The reason your script seems like its stopping is because inside your for loop you have a wait(40) how for loops work is they go in order, they dont all happen at the same time, like itll to this, First lantern do this, wait this much then do this, on to the next lantern. It seems like its not working but it is. To fix this we can use the spawn() to make the code in the for loop run side by side to the for loop.

local light = script.Parent.LIGHT

script.Parent.ClickDetector.MouseClick:connect(function()
    script.Parent.Transparency = 1
    local la = game.Workspace.Lanterns
    local lag = la:GetChildren()
    for _,v in pairs (lag) do
    spawn(function()
            if v.Name == "LanternAtt" then
                local attA = v:FindFirstChild("AttachmentA")
                if attA then
                    local at = attA:FindFirstChild("Attachment")
                    if at then
                        light:Play()
                        at:Destroy()
                        wait(40)
                        game.Workspace.Atmosphere.ParticleEmitter.Enabled = false
                    end
                end
            end
    end)
    end
    wait(100)
    for _,v in pairs (lag) do
        v:Destroy()
    end
end)
Ad

Answer this question