24 Hour System. "os.time()"

Hello! I want to create a 24 hour round based system. I need a way for each server to have the same countdown. I’ll be using the new MessagingService for keeping track of each teams score. How do I go about using the same time across each server?

Thank you!

1 Like

I should suggest looking for a API that has the feature of what you are looking for.

Try this:

os.time() % (24 * 60 * 60) -- seconds of hours within 24 hours, after 86400 seconds, it will hit 0 again.

– note: tired during the time of writing

Thank you @Operatik! Any suggestion on how to create the countdown GUI digits based off those 24 hours?

Don’t rely on os.time() since it can be out of sync, sometimes by hours!

You should contact an external server (preferably your own, but google.com works, too) to get the time.

Personally I would suggest using tick() over os.time() if you want all your servers to be in sync, since the os.time() might be different across the servers.
Edit: consider using os.time(), instead

Regarding the 24 hour counting question, you’d first have to convert the time since epoch (huge amount of seconds since 1970) rounded down to a day. Then you get the current amount of seconds since epoch, and subtract the ‘rounded amount of seconds’ from it. This will result in the amount of seconds you’re in that day, and this can be converted into your 24h clock. (Or countdown system, with 1 extra subtraction.)

In code it would look like the following:

local TIME = tick() --edit: or os.time()?, read thread for more info

--getting seconds left in a day:
local currentExactDay = TIME/86400 --86400 seconds in a day
local roundedDay = math.floor(currentExactDay)
local dayFraction = currentExactDay - roundedDay --value between 0-1
local secondsInDay = math.floor(dayFraction * 86400) --seconds left in a day
local secondsLeft = 86400 - secondsInDay --if you want to make it count down

--converting it for your timer:
local hours = math.floor(secondsLeft/3600)
local minutes = math.floor((secondsLeft%3600)/60)
local seconds = secondsLeft%60

print("hours: "..hours.." minutes: "..minutes.." seconds: "..seconds)

I think this should at least get you going in the right direction.

4 Likes

tick() uses the system’s local time. This varies depending on the server’s timezone. If anything, it would be worse than os.time()!

Thank you for providing so much information!

If you do not want to figure out the time manually, you can always use an API, such as http://worldtimeapi.org/. This way, every server can be in sync no matter what the server has gone through.

2 Likes

Maybe do something with os.date(“!*t”) if I’m correct this will basically be the same for everyone? Not entirely sure

1 Like

My bad, you should probably use os.time() instead, I mixed them up. (I was assuming os.time meant time of the os and therefore was local.)

If you’re using @AbiZinho’s approach, make sure your time function returns os.time() if the API fails, so you always have something to work with.

1 Like

To clarify for anyone who hasn’t used os.date, instead of dividing os.time you can just do os.date("!*t").hour. This probably has the same issues as os.time, but someone should check to be sure.

From the same thread.


1 Like

Pretty sure it’s idd the same problem, however roblox has been trying to fix this since February or so? Not sure how long it’ll take before os.time() (or os.date(“!*t”)) is fixed and synced across all servers