How do I make a timer that restarts itself when it's done?

Later this will be a round-based game but for now, I’m just trying to make a timer for 600 seconds (10 minutes) and after those 10 minutes are up, will reset the timer and start all over again. I’ve managed to make a timer that sets and stops after it reaches 0, but I can’t figure out how to make the timer reset and restart.

Essentially the way I’m trying to make it work is that when a boolean called isDone is true, it will start the timer/call the function. So I start isDone as true so that the script will call the function. The function then will set isDone to false so it isn’t constantly getting called, and then when the timer is done isDone is set back to true so that the timer function is called again. Essentially it’s a loop of the function calling itself. All this does is work once though, after those 10 seconds it doesn’t reset.

local isDone = workspace.isDone.Value
isDone=true

function timer(time)
	isDone=false
	local timeLength=time
	while timeLength> 0  and isDone==false do
		task.wait(1)
		timeLength = timeLength - 1
		print(timeLength)
	end
	print("time is up")

	isDone=true

end

if isDone==true then
	print("starting timer")
	timer(10)
end

I did 10 seconds as it’s easier to test but when it’s in my game it’ll be 600. This code will count down from 10 to 0 and stop as it should, but after that it stops. Does anyone know how to fix this?

give me a minute… ill try to help you in a second, ill test out some code.

Use Modulus [ % ], Modulus essentially is used to return the remainder of something division, however you use use modulus and the number itself, it will return 0, but if you say n+1 % n, it will return 1, completely restarting the process.

You can reverse this effect by making n a negative number, in which it will go from 0 to n, somehing like this:

n = 0; -- current number
max = 100; -- maximum number
print(n + 1 % max) -- 1 (works as normal addition)
print(n % max) -- 0 (works as normal addition)
print(n + (max - 1) % max) -- 99 (works as normal addition)
print(n + max % max) -- 0 (resets back to 0)
print(-n % max) -- 99 ("resets" to 99, the exact opposite direction)

So if your timer is exactly 100 seconds, and the current time goes into the negatives, it will reset back to 99.


If you’re looking to reset the timer as it is, then consider using a loop, so when the function ends, you can can start it back up again:

while (someCondition) do
    func() -- if the function yields, you do not have to worry about adding 
    -- a wait function
end

For a set number of times, you would use a for loop for this occasion:

for i = 1,10 do -- starts at 1, will count up to 10, which runs 10 times
    func()
end

A If statement does not repeat itself, it only fires once to validate whether a specific statement is true, if it is it will run the code inside it, otherwise it will ignore it, for both cases, it will read on past it unless you constantly check.

i cant give my entire code bc its againts forum rules, but let me explain my code to you.

basicly, i did a function that activates while a player is added. (add a debounce so it doesnt constantly fire every time a player is added, just when the server is created.)

then add a while true do loop,


if ID.Value == true then
	ID.Value = false
	wait(1) -- change
	Timer(time)
		end
	end
end)

this should work. if it doesnt, let me know, and i will help you resolve it.

idk why but whatever you did i have no idea and it kinda scares me

1 Like

Using what you already have:

local isDone = workspace.isDone.Value
isDone = true

function timer(time)
	isDone = false
	local timeLength = time
	while timeLength > 0 and isDone == false do
		task.wait(1)
		timeLength = timeLength - 1
		print(timeLength)
	end
	print("time is up")

	isDone = true
	timer(time)  -- Restart the timer
end

if isDone == true then
	print("starting timer")
	timer(10)
end

A smaller version:

while true do
	print("starting timer")
	for t = 10, 1, -1 do
		task.wait(1)
		print(t)
	end
	print("time is up")
end