Making bricks in a model all change colour with one script

Hi everybody,

I have a lot of bricks inside a model, and I want them all to change colour in sync. All the bricks are named the same thing inside the group. I have tried this script…

local Light = script.Parent:GetChildren()

while true do
	wait(0.5)
Light.BrickColor = BrickColor.new("Bright red")
	wait(0.5)
Light.BrickColor = BrickColor.new("Institutional white")
	wait(0.4)
Light.BrickColor = BrickColor.new("Bright yellow")
	wait(0.2)
	Light.BrickColor = BrickColor.new("Bright green")
	wait(0.2)
	Light.BrickColor = BrickColor.new("Bright blue")

end

I have tried a similar script inside an individual brick and worked there, so it must be the line where its identifying all the parts inside the model.

I can’t quite figure out why it doesn’t work, so any help is appreciated - thank you

1 Like

Maybe try…

for i,v in pairs(Light) do
	while true do
	local Coroutine_ = coroutine.create(function()
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Bright red")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Institutional white")
		task.wait(0.4)
		v.BrickColor = BrickColor.new("Bright yellow")
		task.wait(0.2)
		v.BrickColor = BrickColor.new("Bright green")
		task.wait(0.2)
		v.BrickColor = BrickColor.new("Bright blue")
	end)
	coroutine.resume(Coroutine_)
end

This code will not work because :GetChildren() method returns a array of all instances inside a model. To change color for all parts of this model You can use the for loop.

For example

local Model = game.Workspace:WaitForChild("Model")

for _, v in pairs(Model:GetChildren()) do
   --This loop will literate though all children of that model
   if v:IsA("BasePart") then --make sure that v is a part
      --v variable is a part, do something
   end
end
2 Likes

Thanks,

When I put this in, only very few of the lights changed to one colour and stayed on that colour, and others were changing every so often.


local Model = game.Workspace:WaitForChild("MainLights")

for _, v in pairs(Model:GetChildren()) do
	
	if v:IsA("BasePart") then 
		
		v.BrickColor = BrickColor.new("Bright red")
		wait(0.5)
		v.BrickColor = BrickColor.new("Institutional white")
		wait(0.4)
		v.BrickColor = BrickColor.new("Bright yellow")
		wait(0.2)
		v.BrickColor = BrickColor.new("Bright green")
		wait(0.2)
		v.BrickColor = BrickColor.new("Bright blue")
	end
end

This is the exact script I put in, let me know if I did anything wrong

To avoid that only one part at time changes color You need to wrap color changing code in a corountine

local c = coroutine.create(function()
   --That color changing code. Use task.wait() instead of wait()
end)
coroutine.resume(c)
1 Like

Hello PCHDB!

I have written the following code for you, let me know if you still have issues!

local model = game.Workspace:WaitForChild("MainLights",30)
if model then
	while task.wait(1) do --since we are using coroutines, this wait must be equivalent to all the waits in the coroutine
		for i,v in pairs(model:GetChildren()) do
			if v:IsA("BasePart") then
				coroutine.resume(coroutine.create(function()
					v.BrickColor = BrickColor.Random()
					task.wait(1)
				end))
			end
		end
	end
end
1 Like

Ok, thanks.

This does work, however, it runs through the script and then stops on the last colour. Is the script missing something? I tried a while true do but that didn’t seem to make a difference.


local Model = game.Workspace:WaitForChild("MainLights")

for _, v in pairs(Model:GetChildren()) do

	if v:IsA("BasePart") then 
		local c = coroutine.create(function()
		v.BrickColor = BrickColor.new("Bright red")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Institutional white")
		task.wait(0.4)
		v.BrickColor = BrickColor.new("Bright yellow")
		task.wait(0.2)
		v.BrickColor = BrickColor.new("Bright green")
		task.wait(0.2)
		v.BrickColor = BrickColor.new("Bright blue")

		end)
		coroutine.resume(c)
	end
end

Here’s the script again this time :slight_smile:

Tried this and it changed all the parts to random colours at different times. The idea was to have all the parts changing colour, but they all change to the same colour at the same time. Thanks

You need to just wrap that for loop inside a while task.wait() do loop. Keep in mind that you need to add a delay because for loop will end before parts color will change to the last one

Thank you. Where would I add these into the script to make sure it works properly?

Sorry for the questions and thanks for the help :smiley:

local Model = game.Workspace:WaitForChild("ModelLights")

while task.wait(1.3) do --I added up all delays in the part changing code
   --for loop goes here
end
1 Like

Thanks again,

Implemented that into the scrip and it does now loop, however there is a strange flash of red just before the end of each ‘colour cycle’? Any way to fix this?

Full script again for reference

local Model = game.Workspace:WaitForChild("MainLights")

while task.wait(2) do
for _, v in pairs(Model:GetChildren()) do
	
	if v:IsA("BasePart") then 
		local c = coroutine.create(function()
		v.BrickColor = BrickColor.new("Bright red")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Institutional white")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Bright yellow")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Bright green")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Bright blue")

		end)
		coroutine.resume(c)
	end
	end
end

Just increase the time in while loop

1 Like

I think I have already, as I changed them all to 0.5. Also just tried them on 1 second each and no results there. I did also change the while task.wait() amount to be the sum of all the timings.

Hopefully we’ll be able to get this working soon so I dont have to ask many more questions. Apologies and thank you once again :))

By increasing time in while loop i meant that time in while task.wait() do loop to be sum of all delays + additional delay

1 Like

Awesome, works perfectly as far as I can see - thank you so much for the help!!

Final working script in case anybody else is looking for it. Credit: @KRZYMEN

local Model = game.Workspace:WaitForChild("MainLights")

while task.wait(2.5) do
for _, v in pairs(Model:GetChildren()) do
	
	if v:IsA("BasePart") then 
		local c = coroutine.create(function()
		v.BrickColor = BrickColor.new("Bright red")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Institutional white")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Bright yellow")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Bright green")
		task.wait(0.5)
		v.BrickColor = BrickColor.new("Bright blue")

		end)
		coroutine.resume(c)
	end
	end
end
3 Likes