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

courtine while loop help?

I'm trying to make it so it fades the colors from red to green to blue but it gets stuck on green without any errors

local startgreen = false
local startred = true
local startblue = false

local redvalue = script.Parent.TextColor3.r
local greenvalue = script.Parent.TextColor3.g
local bluevalue = script.Parent.TextColor3.b

--red
coroutine.wrap(function()
	while startred == true do
		for i = 0, 255, 5 do
			script.Parent.TextColor3 = Color3.new(i/255,greenvalue,bluevalue)
			wait(1/30)
		end
		wait(2)
		startgreen = true
		for i = 255,0, -5 do
			script.Parent.TextColor3 = Color3.new(i/255,greenvalue,bluevalue)
			wait(1/30)
		end
		startred = false
	end
end)()
--green
coroutine.wrap(function()
	while startgreen == true do
		for i = 0, 255, 5 do
			script.Parent.TextColor3 = Color3.new(redvalue,i/255,bluevalue)
			wait(1/30)
		end
		wait(2)
		startblue = true
		for i = 255,0, -5 do
			script.Parent.TextColor3 = Color3.new(redvalue,i/255,bluevalue)
			wait(1/30)
		end
		startgreen = false
	end
end)()
--blue
coroutine.wrap(function()
	while startblue == true do
		for i = 0, 255, 5 do
			script.Parent.TextColor3 = Color3.new(redvalue,greenvalue,i/255)
			wait(1/30)
		end
		wait(2)
		startred = true
		for i = 255,0, -5 do
			script.Parent.TextColor3 = Color3.new(redvalue,greenvalue,i/255)
			wait(1/30)
		end
		startblue = false
	end
end)()

1 answer

✓ Accepted answer

There are a few problems with your script.

The first one is that you're using while loops. While loops will end if the condition is not true already. This is the reason why the green and blue fading does not occur, they are false and the while loop ends because the condition is not met.

You should resort to a repeat loop that waits until a variable becomes true, that way the function won't end if the variable isn't true to begin with.

The second one is that you're setting properties as variables. While you can do this and it is quicker, things tend to become a bit glitchy with your script and the fading does not work smoothly because variables save a local copy of the value of the property and will not update. It's best to just reference them directly in this case.

I also recommend to use the delay function instead of coroutines. You need to be careful when using coroutines as delay could also provide the same effect as them, thus making coroutines less useful because you're typing more code out.


Final code:

local startgreen = false
local startred = true
local startblue = false
local fadedone = false --Variable that handles when the fading is done.

--red
while wait() do --Infinite while loop.
	repeat wait() until fadedone --Wait until the fading is done.
	fadedone = false --Sets the fadedone variable to false.
	delay(0,function() --Delay by 0 seconds, starts function instantly in a new thread.
		repeat wait() until startred --Wait until startred is true.
		for i = 0, 255, 5 do
			script.Parent.TextColor3 = Color3.new(i/255,script.Parent.TextColor3.g,script.Parent.TextColor3.b)
			wait()
		end
		wait(2)
		startgreen = true
		print(startgreen)
		for i = 255,0, -5 do
			script.Parent.TextColor3 = Color3.new(i/255,script.Parent.TextColor3.g,script.Parent.TextColor3.b)
			wait()
		end
		startred = false
	end)
	--green
	delay(0,function()
		repeat wait() until startgreen --Wait until startgreen is true.
		for i = 0, 255, 5 do
			script.Parent.TextColor3 = Color3.new(script.Parent.TextColor3.r,i/255,script.Parent.TextColor3.b)
			wait()
		end
		wait(2)
		startblue = true
		for i = 255,0, -5 do
			script.Parent.TextColor3 = Color3.new(script.Parent.TextColor3.r,i/255,script.Parent.TextColor3.b)
			wait()
		end
		startgreen = false
	end)
	--blue
	delay(0,function()
		repeat wait() until startblue --Wait until startblue is true
		for i = 0, 255, 5 do
			script.Parent.TextColor3 = Color3.new(script.Parent.TextColor3.r,script.Parent.TextColor3.g,i/255)
			wait()
		end
		wait(2)
		startred = true
		fadedone = true --Tell the script the fading is done.
		for i = 255,0, -5 do
			script.Parent.TextColor3 = Color3.new(script.Parent.TextColor3.r,script.Parent.TextColor3.g,i/255)
			wait()
		end
		startblue = false
	end)
end

I hope my answer helped you. If it did, be sure to accept it.

2 comments

0

i c i c, although you helped making all the fading, the whole fading the the right color doesn't work and it just ends on black without looping

0

I'll edit my script so it does this continuously.

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