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:
01 | local event = script.Parent.Parent.SirenEventOn |
02 | local leftsiren = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.LeftLight |
03 | local rightsiren = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.RightLight |
04 | local leftlight = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.LeftLight.Light |
05 | local rightlight = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.RightLight.Light |
06 | local db = false |
07 | event.OnServerEvent:Connect( function (player) |
08 | if not db then |
09 | db = true |
10 | wait( 0.5 ) |
11 | for i,v in pairs do |
12 | leftsiren.Transparency = 0 |
13 | leftlight.Enabled = true |
14 | rightsiren.Transparency = 1 |
15 | rightlight.Enabled = false |
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:
01 | local db = false |
02 | part.ClickDetector.MouseClick:Connect( function () |
03 | --Used a ClickDetector for simplicity |
04 | if not db then |
05 | db = true |
06 | --wait here wasn't necessary |
07 | while db do |
08 | wait( 0.5 ) |
09 | if db = = false then |
10 | break |
11 | end |
12 | --added another wait here for two reasons |
13 | -- 1. having only one wait will cause it to instantly turn off and back on |
14 | -- 2. if you add this wait on the bottom, the second thread (user pressed f again)may not stop this loop in time |
15 | leftsiren.Transparency = 0 |
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.