Days, Hours, Minutes, Seconds Timer Not Working as

Hey everyone! Sorry for the second post tonight.

I’m having an issue with this system that I got from @FieryEvent on another DevForum post.

My script is here:

local s = 604800

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

It never prints anything.

Thanks!

2 Likes

Try:

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

local s = 604800

print(toHMS(s)) --or print(toHMS(604800))
6 Likes

Sweet! This worked. Now I was wondering, what would you use to make a system where it updates each second? (While loop, or different, more efficient alternative?)

Also, how could I add a “Days” section? Sorry for so many questions, I’m pretty new to doing these types of things.

604800 seconds is a week. 86400 seconds is a day. Divide your time by 86400 and math.floor it. Add an extra placeholder in your format string of %.2i and add the calculation to the beginning.

You will also need to change the hour calculation to use modulus against 24.

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

local s = 604800

print(toHMS(s)) -- 07:00:00:00
1 Like
local start = 8*3600 --time offset, when the first day started (8:00 AM)
local t0 = tick() - start

... --toHMS, setText

local update()
	return tick() - t0
end

local resolution = 1 --update and show updates for 1 second

while true do
	setText(toHMS(update()))
	wait(resolution)
end

My solution was intented for a quick way to format, if you want to format for days and others, use os.date (wiki, lua.org).

Hey there! So yeah, I am going to do a week-long deal in my game for a pass. Thanks though for your concern! :slight_smile:

Also, I will try this too.

Hey there! So I tested your method and, for some reason, this part:

was having issues. It said to remove the perentheses, then I saw that it looked like maybe it was meant to be a local function, so I put “local function update”, and it underlined

Thanks!

Try tick() instead of totick(), I’m pretty sure that’s what he meant to do.

2 Likes

Hey there! So I tried using a that method (idk if you know how to fix this) and here it is:

local start = 604800 --time offset, when the first day started (8:00 AM)
local t0 = tick() - start

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

local function setText(t)
	script.Parent.TextLabel.Text = t
end

local function update()
	return tick() - t0
end

local resolution = 1 --update and show updates for 1 second

while true do
	setText(toHMS(update()))
	wait(resolution)
end

but it goes up every time? Thanks!

Yeah, his code will count up from a specific time. If you want to count down until a specific time, you can take advantage of os.time’s optional parameter.

local dateTable = {month = 10, day = 5, year = 2019}

local t0 = os.time(dateTable)
1 Like