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!
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.
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.
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
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