Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[solved] How would I make this timer work properly using math?

while wait(1) do
	local Hours = math.floor((Time - os.time()) / 3600)
	local Minutes = math.floor(((Time - os.time()) / 60) / Hours)
	local Seconds = math.floor(Time - os.time() / Minutes)
end

I am trying to make a countdown that displays hours, minutes and seconds. But it doesn't work because my math is wrong somewhere. But I do not know where. How would I fix this?

Time is os.time 24 hours in the future, so i need to countdown.

2 answers

Your are correct, the maths is wrong. I'll provide a function i use to get hours, minutes and seconds

local function GetHMS(number)
	local hours = 0
	local minutes = 0
	local seconds = 0
	if number >= 3600 then
		repeat
			number = number - 3600
			hours = hours + 1
		until number < 3600
	end
	if number >= 60 then
		repeat
			number = number - 60
			minutes = minutes + 1
		until number < 60
	end
	seconds = number
	return {hours,minutes,seconds}
end

GetHMS(math.floor(Time - os.time()))

I figured it out myself,

while wait(1) do
	local Hours = math.floor((Time - os.time()) / 3600)
	local Minutes = math.floor((Time - os.time()) / 60) % 60 
	Timer.Text = Hours..":"..Minutes
end

This discussion is preserved as it was. New answers, comments, and votes are closed.