Lights don't change inside model when using GetChildren

local Pod1LED = script.Parent.LED1:GetChildren()

local Pod2LED = script.Parent.LED2:GetChildren()

local Pod3LED = script.Parent.LED3:GetChildren()

local Pod4LED = script.Parent.LED4:GetChildren()

local LEDLoop = 0

repeat

Pod1LED.BrickColor = BrickColor.White()

Pod2LED.BrickColor = BrickColor.Black()

Pod3LED.BrickColor = BrickColor.Black()

Pod4LED.BrickColor = BrickColor.Black()

wait(0.2)

Pod1LED.BrickColor = BrickColor.Black()

Pod2LED.BrickColor = BrickColor.White()

Pod3LED.BrickColor = BrickColor.Black()

Pod4LED.BrickColor = BrickColor.Black()

wait(0.2)

Pod1LED.BrickColor = BrickColor.Black()

Pod2LED.BrickColor = BrickColor.Black()

Pod3LED.BrickColor = BrickColor.White()

Pod4LED.BrickColor = BrickColor.Black()

wait(0.2)

Pod1LED.BrickColor = BrickColor.Black()

Pod2LED.BrickColor = BrickColor.Black()

Pod3LED.BrickColor = BrickColor.Black()

Pod4LED.BrickColor = BrickColor.White()

wait(0.2)

LEDLoop = LEDLoop + 1

print ("Current Loop Count is " .. LEDLoop)

until LEDLoop == 10

It does print the current loop part until LEDLoop == 10, but it doesnt chagne the lights. Can somebody help me?

1 Like

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:

local Pod1LED = script.Parent.LED1
local Pod2LED = script.Parent.LED2
local Pod3LED = script.Parent.LED3
local Pod4LED = script.Parent.LED4
while Repeat = 1, 10 do
    Pod1LED.BrickColor = BrickColor.White()
    Pod2LED.BrickColor = BrickColor.Black()
    Pod3LED.BrickColor = BrickColor.Black()
    Pod4LED.BrickColor = BrickColor.Black()
    wait(.2)
    Pod1LED.BrickColor = BrickColor.Black()
    Pod2LED.BrickColor = BrickColor.White()
    Pod3LED.BrickColor = BrickColor.Black()
    Pod4LED.BrickColor = BrickColor.Black()
    wait(.2)
    Pod1LED.BrickColor = BrickColor.Black()
    Pod2LED.BrickColor = BrickColor.Black()
    Pod3LED.BrickColor = BrickColor.White()
    Pod4LED.BrickColor = BrickColor.Black()
    wait(.2)
    Pod1LED.BrickColor = BrickColor.Black()
    Pod2LED.BrickColor = BrickColor.Black()
    Pod3LED.BrickColor = BrickColor.Black()
    Pod4LED.BrickColor = BrickColor.White()
    wait(.2)
end

Mobile sucks send help

2 Likes

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

Extra Documents:

2 Likes

That doesn’t work for me, i’ve tried it in studio.

Doesn’t work for me in studio.

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?

1 Like

Doesn’t change anything and nothing prints when I put a print command below the colour changing part of the script.

try this one:

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

The script doesn’t work still.

Where is the script ran? Is it a Server or Local? (And just to be sure) do the LED parts have a BrickColor property and are not Light Objects?

Ok, after a few more minutes debugging I realized I accidentally added unnecessary things

Remove this:

if not obj:IsA("BasePart") then continue end -- this would only check if it was a base part smh

Replace this:

if ledform:format(nd) == obj.Name then -- didn't account for if "nd" was nil

With this:

if nd and (ledform:format(nd) == obj.Name) then -- checks for that

Let me know how this works out

1 Like

The script works, but only for one of the LEDS.

1 Like

Ok, after debugging a little more, I got it:

Replace:

if nd and (ledform:format(nd) == obj.Name) then

With

if nd and (ledform:format(obj.Name:sub(nd, nd)) == obj.Name) then -- gets the number at the place it was found
1 Like