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.
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?
01 | gc = game.Workspace.NLA:GetChildren() |
02 |
03 | while true do |
04 | wait( 1 ) |
05 | if game.Workspace.Config.OilPressure.Value > 3499 or game.Workspace.Config.GasPressure.Value > 3499 then |
06 | on = true |
07 | for i,v in pairs (gc) do |
08 | if v.Name = = "NauticalLightAlarm" then |
09 | while on do |
10 | v.Light.PointLight.Enabled = true |
11 | v.Light.Material = "Neon" |
12 | v.Light.BrickColor = BrickColor.new( "Really red" ) |
13 | wait( 0.75 ) |
14 | v.Light.PointLight.Enabled = false |
15 | v.Light.Material = "SmoothPlastic" |
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.
01 | gc = game.Workspace.NLA:GetChildren() |
02 |
03 | while true do |
04 | wait( 1 ) |
05 | if game.Workspace.Config.OilPressure.Value > 3499 or game.Workspace.Config.GasPressure.Value > 3499 then |
06 | on = true |
07 | --First, the greater 'while on do' needs to be top-level! |
08 | while on do |
09 | --Next, we use a for loop to modify all the properties at once... |
10 | for i, v in pairs (gc) do |
11 | if v.Name = = "NauticalLightAlarm" then |
12 | v.Light.PointLight.Enabled = true |
13 | v.Light.Material = "Neon" |
14 | v.Light.BrickColor = BrickColor.new( "Really red" ) |
15 | end |
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