Server clock acting funky

I’m using this script to update a Gui’s text to keep track of how long a server has been up, the problem I’m running into is that minutes can go above 60, so I’ll have a weird clock where it’ll 01:72:49 where 72 is the minutes. I’d rather they reset back to 0 like it does when seconds get above 60 but I’m not sure how to do that.

image

You should be using modulus on minutes as well.

local minutes = math.floor(time/60)%60

Normally I calculate backwards, if you want my two cents on this.

local hours = math.floor(seconds/3600)
local minutes = math.floor((seconds/60) - (hours*3600))
local seconds = math.floor(seconds - (hours*3600) - (minutes*60))

By the way, if you want to get rid of that mass of if statements at the bottom to account for the 0 in an HH:MM:SS format, use zero padding instead.

Gui.Text = string.format("%02d:%02d:%02d", hours, minutes, seconds)

This will pad your numbers by 0 if they’re under the threshold.

2 Likes

This does not need any module, ngl, you guys are making it way to hard, I don’t say your doing wrong, but thats a very hard way for doing it

Just do this simple way:

local startingtime = tick()

local function formatnumber(number)
  return string.format("%02i",number)
end

while task.wait(1) do
   local totaltimetaken = tick() - startingtime
   local hours = math.floor(totaltimetaken/3600)
   local mins = math.floor(totaltimetaken - (hours*3600))
   local seconds = math.round((totaltimetaken - (hours*3600) - (mins*60))/60
   Gui.Text = formatnumber(hours)..":"..formatnumber(mins)..":"..formatnumber(seconds)
end

Proof that this works: (without using any module)



Also @colbert2677 modules shouldn’t be recommended for small stuff like these, this will cause unnesscesary confusion to the OP, and modules should be used for returning exact 1 value

Mostly module scripts are used for large stuff, which prevents RY, Repeating yourself, like you type a code like a function many times, but here, your getting the time time different values, and your only formatting it, you don’t really need to make another module script for a tiny formatting task

He said modulus (this operator %) not module.

btw just formatting the string would be shorter and more readable than using that format function for each number:
string.format("%02d:%02d:%02d", hours, minutes, seconds)

2 Likes

Oh sorry I thought he said module