Brick Color Rotations

Greetings, Im trying to make it so my lamp post switch from the color Green to Red every couple of seconds however, I can’t figure out how to make it loop. It will work without the loop but I don’t want them to stay one color.

	for _,object in pairs(placeToScan:GetChildren()) do
		scanInWorkspace(object)
		if object:IsA("BasePart") and object.Name == "Meshes/lamba_Cube.004" then
			while true do
				object.BrickColor = BrickColor.new("Bright green")
				wait(2)
				object.BrickColor = BrickColor.new("Really red")
			end
	end
end

scanInWorkspace(workspace)```

Any help will be much appreciated!

It looks like you’re setting BrickColor to Green, waiting 2 seconds then setting it to red, then quickly back to green. An easy fix would just adding another wait(2) after you set it back to red.

Use coroutine.wrap():

coroutine.wrap(function()
	while true do
		object.BrickColor = BrickColor.new("Bright green")
		task.wait(2)
		object.BrickColor = BrickColor.new("Really red")
		task.wait(2)
	end
end)()

It allows for multiple threads therefore multiple loops for the different objects.

Also, you should use task.wait() instead of just wait():

1 Like

That worked however, only one of the parts are changing colors now.

Use mine, it uses coroutines which allow for multiple to be coloured.

Yes, that’s because the loop is infinite and is holding up the for loop. @Katrist’s code pushes the code into a new thread so the for loop can finish.

This did it, thansk man! I appreciate the help!