Realistic Time Syncing (Syncs lighting time to local time)

Hello, DevForum!

Today, I would like to share with you kind of a basic thing, but may be useful to some people.
So, today I decided to code a time synchronization client script.

local RunService = game:GetService("RunService")
local Lighting = game:GetService("Lighting")

RunService.RenderStepped:Connect(function()
	--gets current time
	local CurrentTime = os.time()
	
	--variablize the current hour, minute, and second
	local Hour = os.date("%H", CurrentTime)
	local Minute = os.date("%M", CurrentTime)
	local Second = os.date("%S", CurrentTime)
	
	--put it onto the TimeOfDay property of Lighting
	Lighting.TimeOfDay = Hour .. ":" .. Minute .. ":" .. Second
end)

A little explanation of the code is that it gets a player’s local time, puts it in a variable, then gets the hour, minute, and second. After that, it puts them all together on the TimeOfDay Lighting property.

Good for people who want a realistic game.

That’s about it! :slight_smile:

19 Likes

Thanks for making this! I can already see how fun this would be to use, but I have a few questions.
What type of script should we use? And where do we put the script?

3 Likes

Well, the type of script that is most fitted for this is a LocalScript. That’s just the way I did it, but it can most likely also be used on normal Scripts too. You can put the script in a place that a LocalScript can function, but I put it in StarterPlayerScripts.

3 Likes

Thanks a bundle! I’ll be sure to try this script out tomorrow, as it’s currently one in the morning.

2 Likes

Thanks.

Whenever you use the script, tell me if there’s any bugs with it.

3 Likes

I tried to implement this system, but it’s very complicated, but thanx for posting this more helpful one

3 Likes

Well, if you ever have any problems with this system working, you can do a few things.

  • Put the code in a LocalScript

  • Put it in a client-supported area such as StarterPlayerScripts

  • Make sure no other code is overwriting the ClockTime or TimeOfDay property.

Hope this helps. :slight_smile:

4 Likes

The time zone ROBLOX uses is UTC. I wanted to clarify that. But what I think the script is doing is going off of the server’s time zone based on its location.

4 Likes

Tried it out today, worked wonderfully! Got to see it lined up correctly with my clock.

1 Like

It does it based on the player’s timezone, like my timezone is GST, when it’s 6:29 PM in my time, it’s 6:29 PM in the game

3 Likes

Yes, that’s the way I did it. It functions perfectly off my timezone.

3 Likes

Great to see! I’ll most likely be doing more community resource stuff.

2 Likes

But it’s also a server script, which means it’d be going off of the server’s time zone.

1 Like

That could be possible. But, I made it specifically for the client. Not for the server, the client.

1 Like

Well, I would have thought it was for the client if you had replaced “Heartbeat” with “RenderStepped”, because RenderStepped is for the client.

1 Like

In my opinion, RenderStepped is best used for camera and other things that need to be constantly updated.

The time does not need to be updated constantly, because it goes by second and second, not constantly.

That’s why I used Heartbeat.

2 Likes

Both of these are constantly updated, once every frame. Just at a different priority order.
(Hearbeat goes last)

2 Likes

As far as I know, it is best to always use PreRender (RenderStepped) for client, since it is loaded in a certain order.

And also if you don’t like frequent updates from RenderStepped, you can use:

local Lighting = game:GetService("Lighting")

while task.wait(1) do
	local CurrentTime = os.time()

	local Hour = os.date("%H", CurrentTime)
	local Minute = os.date("%M", CurrentTime)
	local Second = os.date("%S", CurrentTime)

	Lighting.TimeOfDay = Hour .. ":" .. Minute .. ":" .. Second
end

The result will be the same.

4 Likes

Hey, I was wondering if I can make it into a server script somehow and it would run server wide for the timezone set by the developers, is that possible?

1 Like

This isn’t a very efficient script. I made this one a while back:

wait(2)
while true do
	game.Lighting.TimeOfDay = os.date("%X")
	wait(30)
end

You can see its age because of how it uses wait. This resource is quite pointless to provide a script that can be shortened to a few lines, but its existence shall be positively acknowledged by the newcomers.