Having all parts change at once ( spawn() not working* )

Why does this not do each item at the same time? It only does them one-by-one, which I thought the spawn() is supposed to fix.
I already tried asking at Hidden Developers, however they’re just talking in the chat.

        spawn(function() -- Added an extra one just to see if it'd fix the problem
        for _,LEDFolder in pairs(game.Workspace.PureLEDs:GetChildren()) do
        
            if table.find(SelectedLEDs, LEDFolder.Name) then
                
                spawn(function()
                for _,LED in pairs(LEDFolder:GetChildren()) do
                    LED.SurfaceLight.Color = Color
                    LED.mainDesign.Design.BackgroundColor3 = Color
                    wait(.5)
                    LED.SurfaceLight.Color = Color3.fromRGB(0,0,0)
                    LED.mainDesign.Design.BackgroundColor3 = Color3.fromRGB(0,0,0)
                                        --Removed rest for no spam
                    
                end
                
            end)
            
        end
    end
    
end)

You’re doing it backwards. The spawn should be inside the nested loop. You have it outside.

In this code, spawn can be used once within the second for loop to have them play at the same time.

for _,LEDFolder in pairs(game.Workspace.PureLEDs:GetChildren()) do
    if table.find(SelectedLEDs, LEDFolder.Name) then
        for _,LED in pairs(LEDFolder:GetChildren()) do
            spawn(function()
                LED.SurfaceLight.Color = Color
                LED.mainDesign.Design.BackgroundColor3 = Color
                wait(.5)
                LED.SurfaceLight.Color = Color3.fromRGB(0,0,0)
                LED.mainDesign.Design.BackgroundColor3 = Color3.fromRGB(0,0,0)
            end)
        end
    end
end
1 Like

I have read the two suggestions by incapaz and Awesom3_Eric, and want to inform you of a slightly better approach, as that will reduce your game’s memory consumption, the number of spawn’ed tasks, and hopefully show you that you were very close to a correct solution.

As a wait is … waiting … even when being inside a for-loop, your original for-loop code won’t affect “all objects at once”, because of the wait-for-each-single-iteration.

You actually would need two for-loops, one before the wait, and one after:

if table.find(SelectedLEDs, LEDFolder.Name) then
  spawn(function()
    -- First set all the children of LEDFolder, to the wanted color
    for _,LED in ipairs(LEDFolder:GetChildren()) do
      LED.SurfaceLight.Color = Color
      LED.mainDesign.Design.BackgroundColor3 = Color
    end

    -- Then wait that half second
    wait(0.5)

    -- Lastly again set all children of LEDFolder, to the black color
    for _,LED in ipairs(LEDFolder:GetChildren()) do
      LED.SurfaceLight.Color = Color3.fromRGB(0,0,0)
      LED.mainDesign.Design.BackgroundColor3 = Color3.fromRGB(0,0,0)
    end
  end)
end

As you will discover, once you make lots of code, you get tired of typing (or copying) the same code over and over again. - This is where functions can be used.

So a more optimized implementation of the above, would be:

-- Function to set all children of a given folder (argument 1) to a given color (argument 2)
local function SetColorForAllChildren(LEDfolder, wantedColor)
  for _,LED in ipairs(LEDFolder:GetChildren()) do
    LED.SurfaceLight.Color = wantedColor
    LED.mainDesign.Design.BackgroundColor3 = wantedColor
  end
end

-- ...
if table.find(SelectedLEDs, LEDFolder.Name) then
  spawn(function()
    SetColorForAllChildren(LEDFolder, Color)
    wait(0.5)
    SetColorForAllChildren(LEDFolder, Color3.fromRGB(0,0,0))
  end)
end
1 Like