How would I make the time in game match the local user's OS?

Hi!
So, I hae an in game time system.
It keeps making people think it’s broken because it should be their local time.

The current script is a very simple one.

while true do
	local Hours  = math.floor(game.Lighting:GetMinutesAfterMidnight()/60) --round hours 
	local Minutes = math.floor(game.Lighting:GetMinutesAfterMidnight() - (Hours * 60)) --take the amount of minutes already accounted for with the hours, then use that to get the minutes
	local timeThing = "AM"
	if Hours > 12 then
		timeThing = "PM"
		Hours -= 12
	end
	script.Parent.Text = string.format("⏰: %d:%02d%s",Hours,Minutes,timeThing)
	wait()
end

How would I use os.time to make this sync locally?
Thanks!

1 Like

the Roblox global function “tick()” returns how many seconds since the unix epoch on the local client’s time. This can be used to get the client time as apposed to os.time() which only returns UTC.

This may also help:

This is my time script

while true do
	wait(2)
	script.Parent.Parent.Lighting.ClockTime += 0.02
end

not sure what to do with it.

use task.wait() instead of wait()

wait() is deprecated now or soon to be deprecated

2 Likes

To get the player’s local time, do this:

local function formatTime()
    local t = tick()
    local hours = math.floor(t / 3600) % 24
    local mins = math.floor(t / 60) % 60
    local secs = t % 60
    return string.format("%d:%d:%d", hours, mins, secs) -- edit: fix this line
end

This will return the user’s local time in a string format. You can use this to display on the text label.

1 Like

How would I make the time in game match up? (the sun’s position, etc)

I recommend having variables as actual words, this is bad practice

Thanks for the tip. never knew that!

it is still fairly new, many don’t know it exists yet

you also got task.spawn() and task.delay() replacing spawn() and delay()

1 Like

You can use lighting.TimeOfDay on the client, like so:

while true do
    local clientTime = formatTime() -- this is the function from my earlier reply
    Lighting.TimeOfDay = clientTime
    task.wait(1)
end

local function formatTime()

local t = tick()

local hours = math.floor(t / 3600) % 24

local mins = math.floor(t / 60) % 60

local secs = t % 60

return string.format("d:d:d", hours, mins, secs)

end

while task.wait() do

local clientTime = formatTime() -- this is the function from my earlier reply

game.Lighting.TimeOfDay = clientTime

script.Parent.Text = clientTime

task.wait()

end
1 Like

This isn’t too hard. You can use os.date() with “%X” (local time in HH:MM:SS) and TimeOfDay (a 24-hour string that looks like that).
This is a different, but simpler approach from the other ones.

while true do
    game:GetService("Lighting").TimeOfDay = os.date("%X")
    task.wait()
end
3 Likes

Sorry, on this line:

return string.format("d:d:d", hours, mins, secs)

It should be:

return string.format("%d:%d:%d", hours, mins, secs)
2 Likes