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

Why is the Generic For loop only playing things one by one?

Asked by 9 years ago

For loops really hate me.

This script I made is supposed to make all the alarms in my place play at once when the OilPressure's value is greater than 3499. NLA is a model holding all the alarms, which are all named NauticalLightAlarm.

  • Workspace
    • NLA
      • NauticalLightAlarm
        • Light
          • PointLight
      • NauticalLightAlarm

The problem is that the generic for loop is only making the alarms play one by one in a sequence rather than all at once. When I use a normal for loop, only one alarm plays. How do I fix it?

gc = game.Workspace.NLA:GetChildren()

while true do
    wait(1)
    if game.Workspace.Config.OilPressure.Value > 3499 or game.Workspace.Config.GasPressure.Value > 3499 then
    on = true
    for i,v in pairs(gc) do
        if v.Name == "NauticalLightAlarm" then  
        while on do
        v.Light.PointLight.Enabled = true
        v.Light.Material = "Neon"
        v.Light.BrickColor = BrickColor.new("Really red")
        wait(0.75)
        v.Light.PointLight.Enabled = false
        v.Light.Material = "SmoothPlastic"
        v.Light.BrickColor = BrickColor.new("Dark stone grey")
        wait(0.75)
    end
    end
    end
    end
    end

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

You're making a common "syncing" bug.

You can't do this in a single for loop, simply because each individual loop contains delays. To fix this, you have to move the delays outside of the loops, and re-loop each time.

gc = game.Workspace.NLA:GetChildren()

while true do
    wait(1)
    if game.Workspace.Config.OilPressure.Value > 3499 or game.Workspace.Config.GasPressure.Value > 3499 then
        on = true
        --First, the greater 'while on do' needs to be top-level!
        while on do
            --Next, we use a for loop to modify all the properties at once...
            for i, v in pairs(gc) do
                if v.Name == "NauticalLightAlarm" then
                    v.Light.PointLight.Enabled = true
                    v.Light.Material = "Neon"
                    v.Light.BrickColor = BrickColor.new("Really red")
                end
            end
            ---THEN wait()
            wait(0.75)
            --Then use the for loop again
            for i, v in pairs(gc) do
                if v.Name == "NauticalLightAlarm" then
                    v.Light.PointLight.Enabled = false
                    v.Light.Material = "SmoothPlastic"
                    v.Light.BrickColor = BrickColor.new("Dark stone grey")
                end
            end
            --And the final wait()
            wait(0.75)
        end
    end
end
0
Thanks a lot! ChromeNine 50 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

If you want to learn how to run two code blocks at the same time you can use corroutines... I think this is a good place to learn it:- http://wiki.roblox.com/index.php?title=Thread_scheduler

0
So I can't just use a for loop to make all those alarm play at once? ChromeNine 50 — 9y

Answer this question