While loop breaking stuff in other scripts

I am trying to have a rainbow loop for text.

Heres the loop im using

while wait(0.1) do
	title.TextColor3 = Color3.fromHSV(zigzag(counter),1, 1)
	counter = counter + 0.01
end

But anything I use a while loop or repeat it, it will break stuff in other scripts. Like for example: I have a script that has a timer for the round to start. It will just completely break this, I have no reason why.

Anytime I remove this all the other scripts wear perfectly fine. The script is located in a normal separate script away from others, it is put under a textLabel.

How do I fix this?

For anyone wondering

function zigzag(X) return math.acos(math.cos(X*math.pi))/math.pi end
counter = 0

Try wrapping this in a coroutine and see if the same thing still happens

2 Likes

Can you show the sections of code from the other scripts that this is interfering with?

I would, but I don’t know exactly what its breaking, my guess is that it breaks any “wait” timers that I have.

Use spawn() function

spawn(function()
     while wait(0.1) do
          title.TextColor3 = Color3.fromHSV(zigzag(counter),1, 1)
          counter = counter + 0.01
     end
end)

Hope this helped!

spawn() is deprecated and highly unreliable. Just use coroutines. They are more efficient, and work MUCH better. spawn also has a very slight delay, so coroutines are better.

Then why i am using spawn() and it works just fine!?

I recommend reading this article to help you understand why spawn() is bad practice to use

In short, spawn() is old and can cause delays if a lot are used as the wait it does is the same as wait() which suffers from the same issue when used a lot

As for @vBagxl, I recommend using this to run that loop in its own thread

local ColorLoop = coroutine.wrap(function())
     while wait(0.1) do
          title.TextColor3 = Color3.fromHSV(zigzag(counter),1, 1)
          counter = counter + 0.01
     end
end)

ColorLoop()
1 Like

Oh, i was using spawn() but i think i will be using coroutines.

1 Like