This is a huge bruh moment
I don’t think you should be using a repeat loop in this Instance
Also, just getting the Children alone without putting it inside a loop won’t function right so you’ll need to prob do something like this:
There are lots of ways this code could be optimized
Old Script (Doesn't work)
local ledform = "LED%d" -- name format for the parent "%d" means "digit"
for i, obj in ipairs(script.Parent:GetChildren()) do -- loops through the script's parent's children
if obj.Name == ledform:format(i) then -- checks if the index that the current loop is on is one of the "LED" lights
coroutine.wrap(function() -- wraps this in a coroutine so that it won't yield the thread and the loop can continue on it's own
local children = obj:GetChildren() -- get the LED's children
for k = 1, 10 do -- start a loop that will loop 10 times
for _, v in pairs(children) do -- loop through the children
if v:IsA("BasePart") then -- checks if the object is a basepart
local colors = { BrickColor.White() , BrickColor.Black() }
v.BrickColor = colors[math.random(#colors)] -- set the brick color to a random color in the "colors" table
wait(0.2) -- wait 0.2 seconds
end
end
end
end)() -- calls the coroutine so that it can run the function
end
end
Final Script:
local ledform = "LED%d" -- name format for the parent "%d" means "digit"
for i, obj in pairs(script.Parent:GetChildren()) do -- loops through the script's parent's children
local nd = obj.Name:find("%d") -- gets the digit in the number's name
if nd and (ledform:format(obj.Name:sub(nd, nd)) == obj.Name) then -- checks if the index that the current loop is on is one of the "LED" lights
coroutine.wrap(function() -- wraps this in a coroutine so that it won't yield the thread and the loop can continue on it's own
local children = obj:GetChildren() -- get the LED's children
for k = 1, 10 do -- start a loop that will loop 10 times
for _, v in pairs(children) do -- loop through the children
if v:IsA("BasePart") then -- checks if the object is a basepart
local colors = { BrickColor.White() , BrickColor.Black() }
v.BrickColor = colors[math.random(#colors)] -- set the brick color to a random color in the "colors" table
wait(0.2) -- wait 0.2 seconds
end
end
end
end)() -- calls the coroutine so that it can run the function
end
end
Would changing the BrickColor.Black/White to BrickColor.new("White")/BrickColor.new("Black") make any sort of difference? If not, could you add print statements to see what gets changed or not?
local ledform = "LED%d" -- name format for the parent "%d" means "digit"
for i, obj in ipairs(script.Parent:GetChildren()) do -- loops through the script's parent's children
if not obj:IsA("BasePart") then continue end -- if the object isn't a basepart then skip it
local nd = obj.Name:find("%d") -- gets the number in the object's name
if ledform:format(nd) == obj.Name then -- checks if the index that the current loop is on is one of the "LED" lights
coroutine.wrap(function() -- wraps this in a coroutine so that it won't yield the thread and the loop can continue on it's own
local children = obj:GetChildren() -- get the LED's children
for k = 1, 10 do -- start a loop that will loop 10 times
for _, v in pairs(children) do -- loop through the children
if v:IsA("BasePart") then -- checks if the object is a basepart
local colors = { BrickColor.White() , BrickColor.Black() }
v.BrickColor = colors[math.random(#colors)] -- set the brick color to a random color in the "colors" table
wait(0.2) -- wait 0.2 seconds
end
end
end
end)() -- calls the coroutine so that it can run the function
end
end