How to stop a countdown?

I have this countdown function for a racing game of mine. I was wondering how I could check (during a 120 second countdown) if a variable called raceInProgress was set to false to then stop the countdown.

function countdown(num, prefix)
	for i = 0, num do
		wait(1)
		if prefix then
			announce(prefix .. num - i)
		else
			announce(num - i)
		end
	end
end

How would I go about stopping this countdown early?

You’d basically have another condition in the loop to check if raceInProgress is false, in which case you break out of it:

function countdown(num, prefix)
	for i = 0, num do
		wait(1)
		if not raceInProgress then
			break -- exit out early
		elseif prefix then
			announce(prefix .. num - i)
		else
			announce(num - i)
		end
	end
end
2 Likes