Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a for loop repeat forever?

I am making a rainbow spiral that changes color. Here's my code:

local CService= game:GetService("CollectionService")

local red= CService:GetTagged("Red")
local orange= CService:GetTagged("Orange")
local yellow= CService:GetTagged("Yellow")
local green= CService:GetTagged("Green")
local blue= CService:GetTagged("BLue")
local purple= CService:GetTagged("Purple")

local RedCycle= coroutine.create(function()
	while true do
		for i,v in pairs(red)do
			v.Color= Color3.fromRGB(213,115,61)  --orange
			v.Color= Color3.fromRGB(255,255,0)
			v.Color= Color3.fromRGB(0,255,0) 
			v.Color= Color3.fromRGB(0,0,255)
			v.Color= Color3.fromRGB(255,0,191)
			wait()
		end
	end
end)

local OrangeCycle= coroutine.create(function()
	while true do
		for i,v in pairs(orange)do
			
			v.Color= Color3.fromRGB(255,255,0)
			v.Color= Color3.fromRGB(0,255,0) 
			v.Color= Color3.fromRGB(0,0,255)
			v.Color= Color3.fromRGB(255,0,191)
			v.Color= Color3.fromRGB(255,0,0)
			v.Color= Color3.fromRGB(213,115,61)
			wait()
		end
	end
end)

local YellowCycle= coroutine.create(function()
	while true do
		for i,v in pairs(yellow)do
			v.Color= Color3.fromRGB(0,255,0) 
			v.Color= Color3.fromRGB(0,0,255)
			v.Color= Color3.fromRGB(255,0,191)
			v.Color= Color3.fromRGB(255,0,0)
			v.Color= Color3.fromRGB(213,115,61)
			v.Color= Color3.fromRGB(255,255,0)
			wait()
		end
	end
end)

local GreenCycle= coroutine.create(function()
	while true do
		for i,v in pairs(green)do
			v.Color= Color3.fromRGB(0,0,255)
			v.Color= Color3.fromRGB(255,0,191)
			v.Color= Color3.fromRGB(255,0,0)
			v.Color= Color3.fromRGB(213,115,61)
			v.Color= Color3.fromRGB(255,255,0)
			v.Color= Color3.fromRGB(0,255,0) 
			wait()
		end
	end
end)

local BlueCycle= coroutine.create(function()
	while true do
		for i,v in pairs(blue)do
			v.Color= Color3.fromRGB(255,0,191)
			v.Color= Color3.fromRGB(255,0,0)
			v.Color= Color3.fromRGB(213,115,61)
			v.Color= Color3.fromRGB(255,255,0)
			v.Color= Color3.fromRGB(0,255,0) 
			v.Color= Color3.fromRGB(0,0,255)
			wait(1)
		end
	end
end)

local PurpleCycle= coroutine.create(function()
		while true do
		for i,v in pairs(purple) do
			v.Color= Color3.fromRGB(255,0,0)
			v.Color= Color3.fromRGB(213,115,61)
			v.Color= Color3.fromRGB(255,255,0)
			v.Color= Color3.fromRGB(0,255,0) 
			v.Color= Color3.fromRGB(0,0,255)
			v.Color= Color3.fromRGB(255,0,191)
			wait(1)
		end
	end
end)

coroutine.resume(RedCycle)
coroutine.resume(OrangeCycle)
coroutine.resume(YellowCycle)
coroutine.resume(GreenCycle)
coroutine.resume(BlueCycle)
coroutine.resume(PurpleCycle)


I will explain. I'm using Collection Service to tag certain parts that start with a certain color and put them all in a coroutine where it keeps changing (for example, anything originally with yellow will go to green, blue, purple, red, orange, and back over and over again. However, this only repeats once, and I don't know how to fix it. I've been trying for over an hour. Thanks.

1 answer

✓ Accepted answer

It seems that the code DOES work but I'm assuming that it's changing the color instantly giving the illusion of it only changing once. Maybe a "wait()" in between each color change?

Also, it would simplify your code if instead of using "coroutine.create" to then be followed by "theCoroutine.resume()" you could use "coroutine.wrap" and then call on the coroutine by calling its name like a function -- "theCoroutine()".

If you have any questions or issues, contact me. ;)

2 comments

0

Thanks so much. It's actually working.

0

No problem. :)

This discussion is preserved as it was. New answers, comments, and votes are closed.