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 4 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:

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)

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 — 4y
0
ah, I see so like while true then? Mrmonkeyman120 65 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 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:

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.

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 — 4y
0
Thanks so much! I just had to change a few things, and now it works and is not laggy! :D Mrmonkeyman120 65 — 4y
0
Glad I could help. SteelMettle1 394 — 4y
Ad

Answer this question