Only one child of loop working

Hey! I’m trying to loop through all of a model’s children, and change certain properties. It works, however only for one child. I don’t know what’s causing this, seen that it’s just a basic for i, v loop, and that there isn’t any break function.

game:GetService("SoundService"):WaitForChild("10Countdown"):GetPropertyChangedSignal("Playing"):Connect(function()
			if game:GetService("SoundService"):WaitForChild("10Countdown").Playing == true then
				local WashLightsModel = workspace:WaitForChild("WashLights")
				local repeatCount = 0
				for i, v in pairs(WashLightsModel:GetChildren()) do
					if v.Name == "WashLight" then
						while wait() do
							if repeatCount < 11 then
								v.Head.Lens.Transparency = 0
								v.Head.Lens.Beam1.Enabled = true
								v.Head.Lens.SurfaceLight.Enabled = true
								repeatCount += 1
								wait(0.5)
								v.Head.Lens.Transparency = 1
								v.Head.Lens.Beam1.Enabled = false
								v.Head.Lens.SurfaceLight.Enabled = false
								wait(0.5)
							end
						end
					end
				end
			end
		end)

What is the while wait() for? Could you try getting rid of it and see if that fixes your issue. If that doesn’t work try putting the local repeatCount = 0 in the for loop.

1 Like

The while wait() is there so that it repeats numerous times.

Isn’t that what the for loop should be for?

Not really. The for loop loops through all of the model’s children. It doesn’t repeat anything.

Did you try the possible solutions in my first reply?

Yes, unfortunately it didn’t help.

You made an loop inside another loop. Because of the chronological nature of scripts, each loop have to finish individually before moving onto the next. You can avoid this by creating new threads so that each loop will only yield in their own designated thread.

				for i, v in pairs(WashLightsModel:GetChildren()) do
					if v.Name == "WashLight" then

						task.spawn(function() --enclosed inside this
							while wait() do
								if repeatCount < 11 then
									v.Head.Lens.Transparency = 0
									v.Head.Lens.Beam1.Enabled = true
									v.Head.Lens.SurfaceLight.Enabled = true
									repeatCount += 1
									wait(0.5)
									v.Head.Lens.Transparency = 1
									v.Head.Lens.Beam1.Enabled = false
									v.Head.Lens.SurfaceLight.Enabled = false
									wait(0.5)
								end
							end
						end)

					end
				end
1 Like

Yep, that fixed the issue, thanks. However, another came up with it. Instead of the 10 times I’d like it to repeat, it repeats only about 3 times now. Do you know what that could cause?

Because the variable repeatCount is an upvalue that is being accessed by all of the loops at the same time. So all the loops will count using the same variable, which is no good.

You can fix it by making that variable local within the thread instead like so:

task.spawn(function()
	local repeatCount = 0
	while wait() do
1 Like

Thanks! It works perfectly now.

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