How to make a 2 hour timer until a door opens?

I’ve tried writing up code for a 2 hour timer for a door to open, similar to Nimblz’s “wait in a house for 4 hours to escape.” However, I only was able to figure out how to convert the 2 hours into seconds,

Is there any way to convert it into h:m:s?

1 Like

Just use this function.

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

For your case, if you want the timer to be on a gui to show how much time you have left just do this

local TextLabel = script.Parent -- your text label here

while true do wait()
TextLabel.Text = toHMS(7200)
end
1 Like

Quick question, should I change up anything with this or just add the script you provided above? Btw, the timer is being displayed as a GUI

local rep = game:GetService(“ReplicatedStorage”)
local minutes = rep:WaitForChild(“Minutes”)
local seconds = rep:WaitForChild(“Seconds”)
local text = script.Parent

if seconds.Value <= 7200 then
text.Text = tostring(minutes.Value)…“:0”…tostring(seconds.Value)
else
text.Text = tostring(minutes.Value)…“:”…tostring(seconds.Value)
end

minutes:GetPropertyChangedSignal(“Value”):Connect(function()
if seconds.Value <= 7200 then
text.Text = tostring(minutes.Value)…“:0”…tostring(seconds.Value)
else
text.Text = tostring(minutes.Value)…“:”…tostring(seconds.Value)
end
end)

seconds:GetPropertyChangedSignal(“Value”):Connect(function()
if seconds.Value <= 7200 then
text.Text = tostring(minutes.Value)…“:0”…tostring(seconds.Value)
else
text.Text = tostring(minutes.Value)…“:”…tostring(seconds.Value)
end
end)

1 Like

All you have to do is just copy this script into your textlabel

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

while true do wait()
TextLabel.Text = toHMS(7200)
end
2 Likes

I would do it like this but realised after writing this that this is not what you were asking lol.

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

local endingTime = os.time() + 7200 -- current time plus 2 hours in seconds
while true do
    local diff = endingTime - os.time() -- the difference between the ending time and now
    if diff > 0 then -- still got more time to wait!
         label.Text = toHMS(diff) -- change for your own label
         wait(0.5)
    else
         break -- stop the loop if all time has passed
    end
end

-- open door, or whatever you want to do after 2 hours

EDIT:

To convert seonds to the h:m:s, the function provided by @ScriptedBinary should work fine. Just use toHMS(secondsLeft) and it will convert it into a string for you.

The rest of the code he provided however will just continually show “02:00:00” so I don’t think you want the loop part from his solution.

2 Likes

After reading through, I managed to combine the 2 codes that @ScriptedBinary and @mario118118 wrote, and this should work, place it in a script and it will run and start a 2 hour timer.

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

local length = 7200 -- Time in seconds of how long the timer will be.

while length > 0 do
  label.Text = toHMS(length) -- Make sure to put label as a variable
  length = length - 1
  wait(1)
end
print("Timer ended")

Sorry to ask such an obvious question–due to me being a fairly new scripter–but would you mind giving it a bit more detail (ex: how I should define label) and how to convert 7200 seconds to hours and have it displayed as such? I tried your script and It’s now displaying 0. I’m most likely doing something wrong but I’m not sure what im doing wrong

Sure!, So this code below defines label with a variable (which is created with the word local), and it makes a variable for the time remaining with the function that ScriptedBinary gave.

This script should be inside the textlabel which the timer will be shown on.

local label = script.Parent -- The textlabel
local length = 7200 -- The length of the timer

local function toHMS(s) -- This function converts a certain amount of seconds to hours:minutes:seconds
    return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end

while true do -- We make a loop to continuously check if the time is up
  if length == 0 then -- If the length is equal to 0, then the timer is over, so stop and break out of the loop
    print("Timer has finished")
    label.Text = "Time is up!"
    break
  else
    -- Otherwise, the timer has not finished, so keep going!
    label.Text = toHMS(length) -- Set the text to the hours:minutes:seconds which gets converted
    length = length - 1 -- Make the timer go down
    wait(1) -- Obviously, we have to wait a second so the timer doesn't countdown like a rocket.
  end
end
print("Loop ended!") -- The loop has ended and the script can continue!

There you go! I put comments to help you know what each part of the script does!

2 Likes

Haha! it works! Thank you so much! A true life saver.

No problem! Have a nice day :smiley:

1 Like