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?
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.
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.
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.
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