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!)
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)
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()
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.
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.