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.
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?
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.
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.
Well, I would have thought it was for the client if you had replaced “Heartbeat” with “RenderStepped”, because RenderStepped is for the client.
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
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?
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.