How to make Timer that resets once a Player clicks a GUI?

This is what I have currently.

local length = 7200 

local function toHMS(s)
	return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end

while true do
	if length == 0 then
		print("Timer has finished")
		label.Text = "Time is up!"
		break
	else
		label.Text = toHMS(length) 
		length = length - 1
		wait(1) 
	end
end
print("Loop ended!")

Basically what it does is it automatically counts down for 2 hours until it reaches 0 and stops. But when it reaches 0 I want the player to be able to click a gui button and it will reset to 2 hours again. (PS: I can do the GUI, its just the timer continuation problem im having trouble with!)

3 Likes

Hello there!

This is what I would do:

local length = 7200
local active = false -- When this value is true, the loop will run

local function toHMS(s)
    return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end

while active do -- Run only when "active" is true
    if length == 0 then
	    print("Timer has finished")
	    label.Text = "Time is up!"
	    active = false -- Timer is at 0 so we stop the loop
	else
	    label.Text = toHMS(length) 
	    length = length - 1
	    wait(1) 
    end
end

if Button.MouseButton1Click:Connect(function() -- If the button is pressed
    if active == false then -- If the timer isn't already active
        active = true -- Set timer to active
    end
end)

Remember to define “Button”

Hopefully I helped you! :smiley:

2 Likes

I’ll test this out! update you if it works :smiley:

2 Likes

Just so you know, I updated the script since I made a few errors! :laughing:

2 Likes

you can wrap the code in a function so that the count is repeated.

local length = 7200 
local connection = nil

local function toHMS(s)
	return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end

local function count()
	if connection then
		connection:Disconnect()
		connection = nil
	end
	while true do
		if length == 0 then
			print("Timer has finished")
			label.Text = "Time is up!"
			break
		else
			label.Text = toHMS(length) 
			length = length - 1
			wait(1) 
		end
	end
	connection = button.Activated:Connect(function ()
		length = 7200
		count()
	end)
end

count()
2 Likes

Could you do some commenting on your code? I would like to figure out how it works!

By commenting I meant on the parts u wrote/added in, dont have to comment on the parts I wrote!

I put your counter code in a function called count. Every time count is called the count starts. I guess everything is clear so far.

local function count()
	while true do
		if length == 0 then
			print("Timer has finished")
			label.Text = "Time is up!"
			break
		else
			label.Text = toHMS(length) 
			length = length - 1
			wait(1) 
		end
	end
end

So what we want is that every time the count ends a button is enabled that can do something.

local function count()
	while true do
		if length == 0 then
			print("Timer has finished")
			label.Text = "Time is up!"
			break
		else
			label.Text = toHMS(length) 
			length = length - 1
			wait(1) 
		end
	end
	button.Activated:Connect(function ()
		-- some code
	end)
end

That something will be to reset the count and call count so that a new count starts.

	button.Activated:Connect(function ()
		length = 7200
		count()
	end)

But there is still a problem. Every time the count ends a new connection is created. Also, after the counter is reset once the button will be active and can start a new count parallel to the one that already exists, which will generate some pretty crazy behavior. To avoid that we simply have to destroy the previous connection every time we start a count.

local function count()
	if connection then
		connection:Disconnect()
		connection = nil
	end
	while true do
		if length == 0 then
			print("Timer has finished")
			label.Text = "Time is up!"
			break
		else
			label.Text = toHMS(length) 
			length = length - 1
			wait(1) 
		end
	end
	connection = button.Activated:Connect(function ()
		length = 7200
		count()
	end)
end

Finally we must call count so that everything starts to work.

	end)
end

count()
1 Like

Thank you for your efforts. This problem is resolved now!

1 Like