How can I fix my rainbow above-head-GUI script?

I’m trying to create a script that cycles through the colors of a rainbow, which in turn changes the color of an above-head-rank-nametag. I was able to successfully set up the nametag, however, I’m having trouble with the rainbow part.

While editing, I noticed an error message, however, I don’t understand how to fix it.

Current Script:
local CurrentColor = Color3.fromRGB(255,255,255)

------------------------------------------------------------------------

while true do
	
wait(0.25)

if NextColor == 1 then CurrentColor = Color3.fromRGB(148,0,211)
elseif NextColor == 2 then CurrentColor = Color3.fromRGB(75,0,130)
elseif NextColor == 3 then CurrentColor = Color3.fromRGB(0,0,255)
elseif NextColor == 4 then CurrentColor = Color3.fromRGB(0,255,0)
elseif NextColor == 5 then CurrentColor = Color3.fromRGB(255,255,0)
elseif NextColor == 6 then CurrentColor = Color3.fromRGB(255,127,0)
elseif NextColor == 7 then CurrentColor = Color3.fromRGB(255,0,0)

wait(0.25)
end
end

------------------------------------------------------------------------

	while true do
	script.Parent.Rank.TextColor3 = CurrentColor

	script.Parent.Username.TextColor3 = CurrentColor
wait(0.5)

end

------------------------------------------------------------------------

if NextColor <= 7 then NextColor = NextColor + 1
	
	else
		NextColor = 1

end


In-studio screenshots:

image
(Note that black was never supposed to be a possible color.)

image
(Explorer Tree.)

image
(Script error that I don’t undertand.)


Notes
  • Output shows no errors.

Thanks!

Wow, thanks for everyone’s help! I’m pretty new to scripting, and I learned a lot about Coroutines and tables!

1 Like

Try moving your loops into a spawn function so they don’t cut off the rest of the code from running. Either that, or restructure your code to avoid that entirely.

EDIT:

Coroutines will also work.

3 Likes

I know this is said a lot, but please do not use while true do

You can instead use while wait(.25) do. This accomplishes the same task without the risk of accidentally running through your loop an insane amount of times in one instance and crashing your game.

Onto the fix:

I understand what you are trying to do. It is easier to create an array of the colors you would like to iterate through rather than relying on your variables.

local colors = {Color3.fromRGB(148,0,211), Color3.fromRGB(75,0,130), Color3.fromRGB(0,0,255), Color3.fromRGB(0,255,0), Color3.fromRGB(255,255,0), Color3.fromRGB(255,127,0), Color3.fromRGB(255,0,0)}

Now that you’ve set the colors you would like to reference inside the array, you may call them using colors[num_of_item_in_table]

In Lua, you the first item in an array is 1, so to reference the first color stored you may use colors[1]. Now let’s get into changing the color of the text!

while wait() do
   for i, v in ipairs(colors) do // this is how to iterate thru colors
     script.Parent.Username.TextColor3 = v -- v being the value in the table, aka the color
     wait(.25)
end

Now this is all assuming you have the Color3.fromRGB() settings correct! Hope this helps.

*Edit: The reason your code was not working beforehand was that your code was interrupting itself.

1 Like

The reason, it doesn’t work is because script is stuck in first loop, it repeats it, but doesn’t continue executing/running rest of code. To solve that, put all while loops in

spawn(function ()
    -- your loop here
end)

Note: Do it, for each loop

In my own explanation, it makes code keep executing/running rest of code without stopping on wait() or loop or anything else that makes an some sort of stop.

Or you can use alternative option, using less loops and less spawn()

Best way, is just put everything in one while loop. Less loops. But you will need modify your code a bit.

1 Like

Actually that is very bad advice. Do not use while wait() do. Consult the following for why:

1 Like

Normal scripts should not be a descendant of a player’s PlayerGui. Try this in a local script parented to the ui:

local colours = {
     Color3.fromRGB(148, 0, 211);
     Color3.fromRGB(75, 0, 130);
     Color3.fromRGB(0, 0, 255);
     Color3.fromRGB(0, 255, 0);
     Color3.fromRGB(255, 255, 0);
     Color3.fromRGB(255, 127, 0);
     Color3.fromRGB(255, 0, 0);
}

local current = 0
while true do
    current = (current % 7) + 1
    script.Parent.Rank.TextColor3 = colours[current]
    script.Parent.Username.TextColor3 = colours[current]
    wait(0.5)
end

To explain what this code does, it basically increases current by 1 and makes sure that it stays between 1 and 7 with the modulo operator, the new current will be used as a new index to get a new colour

2 Likes

I don’t believe I’ve had issues with normal scripts being used to operate a player gui although I would agree with it being bad practice. Just a little edit to your code: I recommend using for i,v in next loops as that would be easier to work with although your code should work just fine.

1 Like

Thanks for the info! Forgot long ago why it was bad :thinking:

2 Likes