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,
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)