I’m working on a game and I want to make the lighting/time realistic. How could I synchronize ingame time with real life time using os.date?
This is actually really simple.
local function getTime()
local date = os.date("*t") -- this creates a table with {year = 1998, month = 9, day = 16, yday = 259, wday = 4, hour = 23, min = 48, sec = 10, isdst = false}
return ("%02d:%02d:%02d"):format(((date.hour)), date.min,date.sec) -- gets the hour minutes and seconds within that table and formats it
end
while game:GetService("RunService").RenderStepped:Wait() do
game:GetService("Lighting").TimeOfDay = getTime() -- set the time
end
here’s a few helpful links if you’re not catching on:
Where do I put this script? In Workspace or Lighting, as a LocalScript or Script?
RenderStepped is a client-sided event, so you’ll need to have it as a localscript if you wish to use it.
I’d replace RenderStepped with Heartbeat, though, as RenderStepped is meant to be used for characters & the camera.
Do I put it in Workspace or Lighting?
You can place the localscript in StarterGui / StarterPlayerScripts.
How can I add an hour to that to be CET (UTC+1)?
you would have to format the os.date to UTC instead of your local time then add another hour to it
local date = os.date("!*t")
local UTC1 = date.hour + 1
Or alternatively, you could just add one to the TimeOfDay.
Also, don’t use RenderStepped for this. Seeing as real time moves really slowly, updating once per minute should suffice. Make sure to put it at the end so the script repeats at least once before waiting.
How do I add one to TimeOfDay?
game:GetService(“Lighting”).ClockTime += 1
It’s not working it stays as UTC+0
put this script into a server script into ServerScriptService:
local function getTime()
local date = os.date("!*t") -- this creates a table with {year = 1998, month = 9, day = 16, yday = 259, wday = 4, hour = 23, min = 48, sec = 10, isdst = false}
local UTC1 = date.hour + 1
return ("%02d:%02d:%02d"):format(((UTC1)), date.min,date.sec) -- gets the hour minutes and seconds within that table and formats it
end
while wait(1) do
game:GetService("Lighting").TimeOfDay = getTime()
end
function getTime()
local date = os.date("*t") -- this creates a table with {year = 1998, month = 9, day = 16, yday = 259, wday = 4, hour = 23, min = 48, sec = 10, isdst = false}
return ("%02d:%02d:%02d"):format(((date.hour)), date.min,date.sec) -- gets the hour minutes and seconds within that table and formats it
end
while true do
game:GetService("Lighting").TimeOfDay = getTime() -- set the time
game:GetService("Lighting").ClockTime += 1
wait(3)
end
fwiw, here’s the code from my game. I’m not sure on the performance impact of date formatting, but there is a simpler way to do this
local Lighting = game:GetService("Lighting")
while true do
Lighting.ClockTime = (tick() % 86400) / 3600
wait(60)
end
tick()
is prefered for this, rather than os.time()
as tick()
will always return the time in the player’s timezone, os.time()
will return the date in the UTC timezone.