My problem is pretty simple. I’ve got a for loop to loop through all of the children of a Model. However, it is affecting 1 child at time only.
I’d like it to affect all of them at the same time, meaning that all of the alarm lights should turn on and off at the same time.
You can see the issue in the video below.
local radioEvent = game:GetService("ReplicatedStorage").ToggleRadioWarnLights
local WarningLightsRadio = workspace:WaitForChild("RadioTowerWarningLights")
local RadioTowerModel = workspace:WaitForChild("RadioTower")
local RadioAttachment1 = RadioTowerModel:WaitForChild("Model").ParticlePart.Attachment1
local RadioAttachment2 = RadioTowerModel:WaitForChild("Model").ParticlePart.Attachment2
local RadioAttachment3 = RadioTowerModel:WaitForChild("Model").ParticlePart.Attachment3
radioEvent.OnServerEvent:Connect(function()
for i, v in pairs(RadioAttachment1:GetChildren()) do
v.Enabled = true
task.spawn(function()
for i, v in pairs(WarningLightsRadio:GetChildren()) do
v.Toggle.Value = true
wait(5)
v.Toggle.Value = false
end
end)
task.spawn(function()
for i, v in pairs(RadioAttachment2:GetChildren()) do
v.Enabled = true
wait(1)
v.Enabled = false
end
end)
task.spawn(function()
for i, v in pairs(RadioAttachment3:GetChildren()) do
v.Enabled = true
wait(1)
v.Enabled = false
end
end)
wait(1)
v.Enabled = false
end
end)
Using a wait() in a loop would hold up the script.
Either use coroutines (I am a bit unexperienced with them, so bear with me)
for i, v in pairs(RadioAttachment3:GetChildren()) do
coroutine.resume(coroutine.create(function()
v.Enabled = true
wait(1)
v.Enabled = false
end))
end
or use a function
task.spawn(function()
for i, v in pairs(RadioAttachment3:GetChildren()) do
FlashTheLight(v,1)
end
end)
function FlashTheLight(v,t)
v.Enabled = true
wait(t)
v.Enabled = false
end
radioEvent.OnServerEvent:Connect(function()
task.spawn(function()
for i, v in pairs(RadioAttachment1:GetChildren()) do
v.Enabled = true
end
for i, v in pairs(WarningLightsRadio:GetChildren()) do
v.Toggle.Value = true
end
for i, v in pairs(RadioAttachment2:GetChildren()) do
v.Enabled = true
end
for i, v in pairs(RadioAttachment3:GetChildren()) do
v.Enabled = true
end
task.wait(1)
for i, v in pairs(RadioAttachment1:GetChildren()) do
v.Enabled = false
end
for i, v in pairs(WarningLightsRadio:GetChildren()) do
v.Toggle.Value = false
end
for i, v in pairs(RadioAttachment2:GetChildren()) do
v.Enabled = false
end
for i, v in pairs(RadioAttachment3:GetChildren()) do
v.Enabled = false
end
end)
end)
This removes almost all needs for task.spawn() and makes the code look more clean.