Time & You: A Quick Reference for All Things Roblox and Time

This tutorial is my attempt to document Roblox’s many time-related features. This is in no way comprehensive, or even well-polished, so suggestions and feedback are more than welcome. If you have relevant use cases, caveats, notes or really anything, please reply.


  • 1 microsecond = 1/1,000,000th of a second
  • 1 millisecond = 1/1,000th of a second

Getting time


os.time()

  • Returns the number of seconds since the UNIX epoch
  • Accurate to 1 second

os.clock()

  • Returns the number of seconds of CPU time used by a program
  • Accurate to ~1 microsecond

tick() - Deprecated

  • Returns the number of seconds since the UNIX epoch, on the computer the code was ran on (i.e., do not assume this will be synced across clients or the server)
  • Accurate to 1 second (precision is ?)
  • See this post for deprecation info, tick() has large discrepancies and an alternative should almost always be used

time()

  • Returns the number of seconds since the game began on the computer the code was ran on (i.e., this will not be synced across clients or the server).
  • Accurate to 1/240th of a second (updated every 1/240th, precision may be more)

elapsedTime()

  • Returns the number of seconds since Roblox started running
  • Accurate to 1 millisecond
  • Inconsistent behavior across OSes, can be extremely outdated on mobile

workspace:GetServerTimeNow()

  • Returns the number of seconds since the server began (synced across both client and server)
  • Accurate to 1 microsecond

workspace.DistributedGameTime

  • Returns the number of seconds since the game has been running on the computer the code was ran on (i.e., this will not be synced across clients or the server).
  • Accurate to 1/60th of a second (updated every 1/60th, precision may be more)

DateTime.now().UnixTimestamp

  • Returns the number of seconds since the UNIX epoch
  • Accurate to 1 second

DateTime.now().UnixTimestampMillis

  • Returns the number of milliseconds since the UNIX epoch
  • Accurate to 1 millisecond

RunService.Heartbeat(deltaTime)

  • deltaTime Returns the number of seconds since the last frame
  • Accurate to 1/240th of a second (updated every frame as a parameter of Heartbeat)

RunService.Stepped(time)

  • time returns the number of seconds RunService has been running
  • Accurate to ?

RunService.Stepped(time, deltaTime)

  • deltaTime returns the number of seconds since the last frame
  • Accurate to 1/241th of a second (updated every frame as a parameter of Stepped)

RunService.RenderStepped(deltaTime)

  • deltaTime Returns the number of seconds since the last frame
  • Accurate to ? (updated every frame, precision is ?)

Using time


os.difftime(t2, t1)

  • Returns the difference between two times, t2 and t1 must be integers representing seconds since the UNIX epoch
  • Due to t2 and t1’, this is accurate to 1 second

Thanks

14 Likes

This returns tick() for a specific consistent timezone. That means, it will be the same on the client and the server no matter where in the world they both are.

tick() is more accurate than a second. It goes 4-8 decimals deep from what I remember.

4 Likes

Updated the post, thanks!

The documentation for tick() only mentions it returns the number of seconds, without mentioning precision. Since I can’t find an authoritative source for how precise tick() is, I can’t really say how accurate any of the decimals are.

It’d be great if Roblox made a documentation page like this for us, as it stands the documentation is too vague for me to give any answers without playing it safe.

I’ve edited it to say “precision is ?” but if anyone has a better idea on how to express this uncertainty, that’d be great too.

1 Like

Here’s a tip: you can test how much precision tick has, by simply writing print(tick()) into Roblox Studio’s command bar (Don’t remember if you need to be in playmode or not.)

2 Likes

Forgot to reply to this, sorry!

The problem is there’s no documentation to confirm the accuracy of those returned decimals.

For example, os.clock() returns many decimals, at times well past the millionths place the documentation guarantees accuracy to. These decimals, although given, can’t really be trusted for anything.

I doubt there’s a use-case for beyond microsecond precision in Roblox, but the point stands with other, less accurate methods for getting time like tick().

1 Like

Seems pretty precise to me:

image

print(tick()) --> .020084
print(tick()) --> .020172

print(tick() == tick()) --> true

Thanks for compiling this all here, great to see it all condensed into one place.

2 Likes

Avoid using tick():

1 Like

Thanks! I’ve never really used tick(), so I had no idea. I’ve edited my post.

1 Like

This post has more details on tick().

  • tick() sounds perfect - it has a high resolution (usually around 1 microsecond), and a well-defined baseline - it counts since UNIX epoch! Or, well, it actually doesn’t. On Windows, it returns you a variant of the UNIX timestamp in local time zone. In addition, it can be off by 1 second from the actual, real UNIX timestamp, and might have other idiosyncrasies on non-Windows platforms. We’re going to deprecate this in the future.

Emphasis mine. I was unaware of this info on tick(), I’ve always used an alternative. Regardless, even more so now, there’s no guarantee for the precision of tick(). I’ve edited my post to reflect these details.

1 Like