How do I get the current time in Roblox (consistent across all servers)?

17,811

Solution 1

The Roblox Wiki article discussing tick has recently been updated to include some additional information which answers my question:

Warning: tick() returns local time, and time zones between servers may vary. A reliable option to get the same time on every server is to call os.time().

After Googling for os.time(), I discovered that it's actually part of the standard library for the LUA language. More information about os and os.time can be found here:

Solution 2

Because the ROBLOX servers can use different timezones, you would simply want to use a server where you know what timezone it uses.

To get data from an external server you need to use the HttpService.

If you have your own server, you can use that to get the current time, and if not you can look for a server that can give you the current time.

Here is an example on how you can get the current time:

local TimeServer = "http://www.timeapi.org/utc/now?%25Q"
local CurrentTime = Game:GetService("HttpService"):GetAsync(TimeServer, true)
local ServerTime = tick()

This will make the "CurrentTime" variable contain the current microseconds since Unix Epoch.

You do not want to use HttpService each time you want the time, so you want to use something like this:

function getTime()
    return CurrentTime - (ServerTime * 1000) + tick()
end

But to make it more efficient, place this in a ModuleScript in Game.ServerScriptService called "Time" :

local TimeServer = "http://www.timeapi.org/utc/now?%25Q"
local CurrentTime = tonumber(Game:GetService("HttpService"):GetAsync(TimeServer, true))
local ServerTime = tick()

return function() return (CurrentTime / 1000) - ServerTime + tick() end

Then you can use it in any script like this:

local getTime = require(Game.ServerScriptService.Time)

print(getTime())
wait(1)
print(getTime())

To be noted is that if you call the tick() function from a LocalScript, you will get the player's time, and not the server's. This might not be what you normally want, but it can be used to find what timezone the player is in, or to see what the player's current time is.

Share:
17,811
Ajedi32
Author by

Ajedi32

Full stack web developer/Linux admin/DevOps guy with experience in a broad range of topics including Ruby on Rails, Docker, Bash, JavaScript, PowerShell, git, etc.

Updated on June 09, 2022

Comments

  • Ajedi32
    Ajedi32 about 2 years

    In my Roblox place, I want to get a value representing the current time which is consistent across all running servers and places in my game.

    I already know about the Time function, but the documentation for that function seems to indicate that it is only consistent across a single place (or perhaps only a single place instance?):

    Returns returns the current place time in seconds.
    

    There's also tick:

    Returns the time in UNIX time, which is the number of seconds since the Unix epoch, January 1st, 1970.

    But that has another issue:

    Because some servers run in different timezones (such as servers in America and Europe), tick() will return a different time on those servers. This time will differ in several hours, as much as the time zone difference is. Due this fact, it cannot say anything about relative times between two servers (such as measuring how long it has taken since the last visit of the player).

    How do I get a time value that's consistent across all servers and places in my game? (The actual time in real life would work well for this purpose.)

  • Ajedi32
    Ajedi32 about 10 years
    I finally got a chance to test that out and it works quite well. The Roblox Wiki was recently updated with a better solution though that doesn't rely on external services like timeapi.org.
  • ZombieSpy
    ZombieSpy about 10 years
    @Ajedi32 Ah, os.time() is indeed better