Loop enabling only one value at time

Hello!

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)
1 Like

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

EDIT: Added a time variable to the function.

2 Likes

Better to use task.wait since wait does not use the task scheduler and can cause slow downs in the game since its using a loop for delaying.

1 Like

Here’s a simple way of solving the problem:

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.

1 Like

I’d prefer an easier fix instead of having more loops than I already have. However, thanks anyways of course!

Unfortunately, that didn’t help. Thanks though.

I think imma use it though, because it seems to work. It’s still a lot of loops, but at least it works. Thanks!

Well, it’s the solution I can come up with just based on the code you’ve provided. Glad it solved your problem, though :white_check_mark::wink:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.