Lighting Sequence Loops

Hello all!
I was working away with a script, making lights change colour. I want it to cycle round one of them for a few times, then do the next pattern a few times, then go back to the start. This what I have:

local light = script.Parent.SpotLight


while true do

	light.Color = Color3.fromRGB(46, 153, 252)
	wait (.5)
	light.Color = Color3.fromRGB(252, 105, 0)
	wait (.5)
	light.Color = Color3.fromRGB(63, 252, 42)
	wait (.5)
	light.Color = Color3.fromRGB(252, 0, 4)
		wait (.5)
		
		

				light.Color = Color3.fromRGB(46, 153, 252)
	wait (.1)
	light.Color = Color3.fromRGB(252, 105, 0)
	wait (.1)
	light.Color = Color3.fromRGB(63, 252, 42)
	wait (.1)
	light.Color = Color3.fromRGB(252, 0, 4)
	wait (.1)
		end
	end
end

I would like it to run through the top section for a few times (lets say 4), then the bottom one to run for 10, then back to the start again (so it repeats the 4, then 10, then 4 etc…). Currently it only runs the top one once, then the bottom one once, then cycles like that.
If anyone could let me know how to fix this, that would be fab. Thanks in advance!

1 Like

You should use a for loop to repeat code a set number of times. You can do this like so:

local light = script.Parent.SpotLight
while true do
	for i = 1, 4 do -- Inititate a loop that will repeat four times (you can change the four to however many times you'd like
		print(i) -- Tells you how many times it has looped.
		light.Color = Color3.fromRGB(46, 153, 252)
		wait (.5)
		light.Color = Color3.fromRGB(252, 105, 0)
		wait (.5)
		light.Color = Color3.fromRGB(63, 252, 42)
		wait (.5)
		light.Color = Color3.fromRGB(252, 0, 4)
		wait (.5)
	end
	for i = 1, 10 do -- This loop will repeat 10 times
		print(i) -- Tells you how many times it has looped.
		light.Color = Color3.fromRGB(46, 153, 252)
		wait (.1)
		light.Color = Color3.fromRGB(252, 105, 0)
		wait (.1)
		light.Color = Color3.fromRGB(63, 252, 42)
		wait (.1)
		light.Color = Color3.fromRGB(252, 0, 4)
		wait (.1)
	end
end

I have also attached some documentation on loops and how to use them. Hope this helps :slight_smile:

1 Like

Yeah, thats works really well! Thanks so much! :slight_smile:

1 Like