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

Can i,v in pairs do, and else cannot work together?

Asked by 5 years ago

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:

01local event = script.Parent.Parent.SirenEventOn
02local leftsiren = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.LeftLight
03local rightsiren = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.RightLight
04local leftlight = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.LeftLight.Light
05local rightlight = script.Parent.Parent.Parent.Parent.Parent.Body.Sirens.RightLight.Light
06local db = false
07event.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
View all 30 lines...
0
The way you're using the "for" loop is for iterating through tables. A while statement is preferred if you just want something to run constantly, a set number of times, or until a condition is met. SteelMettle1 394 — 5y
0
ah, I see so like while true then? Mrmonkeyman120 65 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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:

01local db = false
02part.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
View all 38 lines...

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.

0
Also, you'll need to keep your variable declaration at the top. I didn't really want to create all those objects and listen to a sound when we were just trying to test functionality. SteelMettle1 394 — 5y
0
Thanks so much! I just had to change a few things, and now it works and is not laggy! :D Mrmonkeyman120 65 — 5y
0
Glad I could help. SteelMettle1 394 — 5y
Ad

Answer this question