So I have a siren script, and I want it when you press "F", a loop plays of the siren turning off and on, and then when you press "F" again, then it turns off. It works, but I added a loop, i,v in pairs do, but when I added it the script stopped working completely? How would I add a loop, plus the debounce and else? Here is the script:
local event = script.Parent.Parent.SirenEventOn local leftsiren = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.LeftLight local rightsiren = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.RightLight local leftlight = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.LeftLight.Light local rightlight = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.RightLight.Light local db = false event.OnServerEvent:Connect(function(player) if not db then db = true wait(0.5) for i,v in pairs do leftsiren.Transparency = 0 leftlight.Enabled = true rightsiren.Transparency = 1 rightlight.Enabled = false wait(0.5) leftsiren.Transparency = 1 leftlight.Enabled = true rightsiren.Transparency = 0 rightlight.Enabled = false else leftsiren.Transparency = 1 leftlight.Enabled = false rightsiren.Transparency = 1 rightlight.Enabled = false break db = false end end end)
This reply was getting too long to put in a comment.
So, yes you could complete this with a while true do loop. But, you'll need some factor that will break out of that loop. You'll actually be using a second thread in order to break out of the first thread.
Here is some code I wrote that should accomplish this:
local db = false part.ClickDetector.MouseClick:Connect(function() --Used a ClickDetector for simplicity if not db then db = true --wait here wasn't necessary while db do wait(0.5) if db == false then break end --added another wait here for two reasons -- 1. having only one wait will cause it to instantly turn off and back on -- 2. if you add this wait on the bottom, the second thread (user pressed f again)may not stop this loop in time leftsiren.Transparency = 0 leftlight.Enabled = true rightsiren.Transparency = 1 rightlight.Enabled = false wait(0.5) if db == false then break end leftsiren.Transparency = 1 leftlight.Enabled = false rightsiren.Transparency = 0 rightlight.Enabled = true end else db = false -- done immediately so the loop breaks as soon as it sees this wait() -- waits for loop to get finished before returning siren to original state leftsiren.Transparency = 1 leftlight.Enabled = false rightsiren.Transparency = 1 rightlight.Enabled = false end end)
This should work as you had planned. Just change the function to your own server event function and it should do what you need it to do. Those breaks are necessary if you don't want your sirens to glitch out.