# Global Time Across All Servers

Have you ever wanted it so that all servers share the same exact time? Gives a feel of realism. Here is how:

Part 1: Explanation

tick() is our magic ticket. It will return the amount of seconds since Jan 1, 12 AM, 1970. The reason this is great is because it’s the same across all of roblox. This can be used for timed events, and much more.

math.sin() is a trig function that can be used to all sorts of things but in this case can give a smoothly moving number from -1 to 1 as the value you input into it increases.

lighting and SetMinutesAfterMidnight() allows us to modify the current time of the server. Lighting is the service that allows modification of this time.

Part 2: The Script

Make a local script and place in into StarterPlayerScripts or StarterGui. You can name it what you want but here is the code were going to place into it:

``````local lighting = game.Lighting

local timeSlowRate = 250

game:GetService("RunService").Heartbeat:Connect(function()
lighting:SetMinutesAfterMidnight(math.floor(math.sin(tick()/timeSlowRate)*15*60))
end)
``````

Part 3: Why it works

As tick() increases second by second, sin will supply a number from -1 to 1. This number will change based off the amount of increase on tick(). The problem is that with the amount of tick()'s increase, sin will go from -1 to 1 very quickly and will make the day last only about 1 second. We divide tick() to make it slower. In fact, about 250 times slower.

Now we must process this number going from -1 to 1. First we multiply by 15 because that’s the amount of hours in one roblox day(so that we go from 0 hours to 15 hours the back), then we multiply by 60 to convert the hours into second which is what the function uses.

Recap:

The reason this works across all servers(even the whole of roblox) is because the number we are using to go from -1 to 1 is the same for all servers.

7 Likes

If i recall correctly, `tick()` is deprecated (and if it’s not, it soon will be). If you want to achieve the same thing, but not deprecated you can use `os.time()`. (or the more feature rich `DateTime.now` and just get the unix timestamp from it)

Also make sure you aren’t using `while wait()` for this, use of wait() impacts the responsiveness of other parts of your game (thread scheduler throttle threshold), and is all around inaccurate. You should def replace this with heartbeat, and as a matter of fact; because you’re using a global piece of time; you can get away with doing this on the client with more consistent visual updates and not having to spam replicate to every client. Like so:

``````-- in a local script
game:GetService("RunService").Heartbeat:Connect(function()
lighting.ClockTime = blah
end)
``````

Yeah I was thinking about making it use heartbeat. Im pretty sure os.time() is the exact same thing as tick() and I never saw anywhere that said that tick is deprecated.

This was my first tutorial so thanks for the feedback.

• `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.

here ^

1 Like

The global time system does not need to work on such accuracy but it don’t want deprecated code in my tutorial. I don’t really find it a big deal to have to replace like 20 words and my script.

I did update the script and a few things so that it now works off of heartbeat.