How to make countdown not break entire script

so i have a script:

function deployplayers()

end

now if i want to put a countdown in the script for matchmaking purposes

function COOLDOWN()
	for i = script.COOLDOWN_TIME.Value, 1, -1 do
		script.BUTTON.Value.Text = "Cooldown "..i
		script.BUTTON.Value.BackgroundColor3 = Color3.fromRGB(175, 0, 3)
		wait(1)
	end
end

then the script is sadly not responsive during that period of time, but i need it to be responsive in order to implement a voting system. how can i do that?

i dont want the function code to be inside another function, i want the matchmaking to be called seperately (be a seperate function)

3 Likes

Im trying to get a better idea of what you mean, is the script not working at all?

and what is the value of that first parameter.

2 Likes

If I’m understanding the problem correctly, you can just use a coroutine to prevent the thread from yielding.

Instead of calling the COOLDOWN function like this:

COOLDOWN()

You could write it like this:

coroutine.wrap(COOLDOWN)()
2 Likes

10 is the value of the first parameter

1 Like

what exactly is a coroutine wrap? like a function but doesnt “yield”? what does yield mean

1 Like

A coroutine is just a way to run code without it halting the current thread.

“Yielding” means that your code will wait until the function is finished until proceeding, hence, “yield”

And what about the script, is it not working at all? Are there any errors?

no its just that the script is like frozuen during countdown, as if i would have used task.wait()

1 Like

Did you try putting a print to see if its actually frozen or breaking somehow?

To isolate the loop just do.

task.spawn(function()
    for i = script.COOLDOWN_TIME.Value, 1, -1 do
		script.BUTTON.Value.Text = "Cooldown "..i
		script.BUTTON.Value.BackgroundColor3 = Color3.fromRGB(175, 0, 3)
		task.wait(1)
	end
end)

Task.spawn creates another thread process on the CPU, and it’s asynchronous meaning it runs at a difference timing relative to the rest of the script.

You can also just use task.wait() since it should just wait for that function, but if that doesn’t work task.spawn() will.

very interesting. thanks i will try this and get back to you if it works