"While true do" loop inside "for _, in pairs" loop only changes 1 model

I was trying to make the red light flash in all models. However for some reason, it just flashes it in one model. The models are all inside the “Appearance” folder, they all have the same name, “Signal”, and all have the red light part inside of them.

for _, Signal in pairs (script.Parent.Appearance:GetChildren())
   while true do
   Signal.Lights.Red.Transparency = 0
   wait(0.9)
   Signal.Lights.Red.Transparency = 0.9
   wait(0.9)
   end
end

That’s pretty obvious because you are running infinite loop (so nothing after won’t execute) to fix this just put the loop above the for loop

If I do that, the red lights flash at random times out of sync, and I want them all to flash at the same time.

Just use task.spawn(function() to create new thread

local debounceTable = {}

while true do
for _, Signal in pairs (script.Parent.Appearance:GetChildren())
task.spawn(function() 
   if not debounceTable[Signal] then
   debounceTable[Signal] = true
   Signal.Lights.Red.Transparency = 0
   task.wait(0.9)
   Signal.Lights.Red.Transparency = 0.9
   task.wait(0.9)
   debounceTable[Signal] = nil
   end
end)
end
end
1 Like

I’ve never used these before. Could you please do me a favour and modify my code to include that? So that I could understand how to use them.

Gimme second this will entirely skip the loop.

Oh, it’s that simple then. Thank you. It works now.

I just added the “while true do” loop inside of the task spawn function.

Hello, maybe you explain “task.spawn(function()” ?, I am interested

@BuiIdTheBuilder I’ve modified the code (check the latest edit)

And what task.spawn(function() does is that it makes new thread as I’ve already said so basically if you have any yield inside it won’t stop the code from executing

local time = os.time()
task.wait(2)
print(os.time() - time)

local time = os.time()
task.spawn(function()
      task.wait(2)
end)
print(time - os.time())
1 Like

My version:

for _, Signal in pairs (script.Parent.Appearance:GetChildren())
    coroutine.create(function()
        while true do
            Signal.Lights.Red.Transparency = 0
            task.wait(0.9)
            Signal.Lights.Red.Transparency = 0.9
            task.wait(0.9)
        end
    end)
end

Your version is essentially the same as mine I am just using newer task library that is native for Luau compared to coroutines that are from Lua.

Problem with your code is that it won’t respect the yielding and it will just continue running forever.

Yes but, what about an “break” at the end?

for _, Signal in pairs (script.Parent.Appearance:GetChildren())
   while true do
   Signal.Lights.Red.Transparency = 0
   wait(0.9)
   Signal.Lights.Red.Transparency = 0.9
   wait(0.9)
   break
   end
end

You are kinda right sorry I am not that good at explaining these things if you really want to learn more about them take a look on DevHub or here on devforum.

1 Like

This won’t execute both of these things at the same time and it is going to wait until the first object change the transparency and so on

1 Like