How to make TimeOfDay update to be the current time. Without HTTPService

scripting

#1

Hello! So today, I was thinking of making the lighting system and cool stuff to do with it. I decided to use os.date to get the time.

With this I made it so that the TimeOfDay in lighting is the current time.

I know there are probably other tutorials on this. I hardly checked for them. But right away I didn’t see any.
A few of you guys probably know how to do this already, but oh well.

while wait(60) do

    local timeTable = os.date("*t", os.time()) -- Get the time table.

    game.Lighting.TimeOfDay = timeTable.hour..":"..timeTable.min -- The time table is in dictionary format.
    
end
-- {year, month, day, yday, wday, hour, min, sec, isdst}

The isdst is a bool saying wether daylight savings time is on or off.

This is a very compact way of doing this with no math or using HTTPService.

It’s there free to take. It’s just the lines that get the time and set TimeOfDay(Edit: Model was a script, but now its a LocalScript so that in game it should work. When testing in studio server scripts work but in game the os.time is UTC I believe.). ^ :stuck_out_tongue:


#2

Nice. Could also do:

local lighting = game:GetService("Lighting")
game:GetService("RunService"):Connect(function()
	lighting.ClockTime = (tick() / 3600) % 24
end)

#3

Heartbeat/Stepped/RenderStepped*? If all you want is compactness (not recommending):

while wait() do
	game.Lighting:SetMinutesAfterMidnight(os.date("!*t").min)
end

#4

I think using HttpService for something like this is unintuitive anyways.
Nice thing though! I had my game use Central European Time as the TimeOfDay but changed this in another update because it felt like I was disadvantaging people living on the other side of the world.


#6

Hiya there,

Is there anyway that we can set a specific timezone to be used for all players - instead of going off their local timezone? I’d be looking to just use the relevant UK timezone (GMT), rather than setting it per player.

Any advice would be greatly appreciated,
Thanks,
Jake.


#7

Apologies - replied to the wrong person.


#8

You can use os.date() for this purpose, regardless of using it from a client or server.
https://developer.roblox.com/articles/Lua-Libraries/os

Using the formatstring argument “!*t”, you’ll specify to get the time in UCT - which is the same as GMT, as far as get from a quick Google search - Make note of the exclamation mark before *t - without it, you’re basing the time off of the current device’s timezone, while having the exclamation mark makes the time UCT.


#9

Ah brilliant - thank you for your help, also does it matter where I locate the Local Script or not? Thanks


#10

Doesn’t matter, as long as it’s within a location where the LocalScript will run from:

Check out this page to see what locations LocalScripts will run from: https://developer.roblox.com/api-reference/class/LocalScript


#11

That’s brilliant, thank you very much for your prompt support - greatly appreciate it! :grinning:


#12

Apologies to be a pest, just not too sure why it isn’t currently working in my game, if you could advise anything, that’d be much appreciated.

The Code

local timeTable = os.date("!*t", os.time())
game.Lighting.TimeOfDay = timeTable.hour…":"…timeTable.min

Location
Located in Game>ReplicatedFirst

Thanks,
Jake


#13

Your code seems to be using an incorrect format for the TimeOfDay property.
TimeOfDay expects a string following the hh:MM:ss format - that is, hours, minutes, seconds.

You can strip away the second argument of os.date, as seen in sircfenner’s post.
Also mind that this will just set the time once - if you want it to update over time, you’ll want to follow the examples in the first 3 posts of this thread.

local timeTable = os.date("!*t") -- Gets information about the current UCT time.

-- string.format with a format string to match the hh:MM:ss format, using TimeOfDay.
-- Alternatively you can use :SetMinutesAfterMidnight() or ClockTime.
game:GetService("Lighting").TimeOfDay = string.format("%0.2i:%0.2i:%0.2i", timeTable.hour, timeTable.min, timeTable.sec)

#14

Okay, that is brilliant - thank you very much!


#15

Apologies again, I have added the wait functions for one that updates over time but it doesn’t appear to work.

local timeTable = os.date("!*t") – Gets information about the current UCT time.
while wait(60) do
game:GetService(“Lighting”).TimeOfDay = string.format("%0.2i:%0.2i:%0.2i", timeTable.hour, timeTable.min, timeTable.sec)
end


#16

Try making the local time table in the loop. Otherwise it probably won’t update.

while wait(60) do
local timeTable = os.date("!*t") – Gets information about the current UCT time.
game:GetService(“Lighting”).TimeOfDay = string.format("%0.2i:%0.2i:%0.2i", timeTable.hour, timeTable.min, timeTable.sec)
end

#17

Will give it a go, for this type of script do you recommend a particular location to place it in? Which one works for you?


#18

I just have it in StarterGui. I figure if I ever want to make a gui displaying time of day, I already have a table for it.


#20

Just seen your original post about studio instances, will give it a go in-game.


#21

If your looking for utc time. I think a server script would work. Although if your using a local script make sure that your checking the clients TimeOfDay and not the servers(Due to FE). Like if your testing in game and using the command bar to check the time of day. It will display 14:00:00 if you haven’t changed it on the server.


#22

OK, the updated script with the wait times (that you supplied earlier) do not appear to work, however, the one without the updating facility appears to work by setting the time once. It could be what you said and I’ll look into it now.