How do I make a real time day/night cycle?

So my question is how to make a real time night cycle (for example in MEZ time zone)
It shouldn’t be influenced by the game when the time starts but the time in the game should be like in real life.
I tried to search tutorials on youtube but I didn’t found any I also searched in Developer Forum.

Thanks for reading, I hope someone is able to help me.

8 Likes

Use the DateTime object.

local dateTime = DateTime.now()
local universalTime = dateTime:ToUniversalTime()
game.Lighting.ClockTime = universalTime.Hour

If you want it specific to a timezone, you must add or subtract the hours to get it. For example, CST is -6 so

local dateTime = DateTime.now()
local universalTime = dateTime:ToUniversalTime()
local cstTime = universalTime.Hour - 6
game.Lighting.ClockTime = cstTime 
8 Likes

how do I do it then that the time also changes?

1 Like

The time will change automatically because it’s referencing DateTime and updating the Lighting property.

2 Likes

But shouldn’t this be in a loop? As the code will run once and set the time to the time local dateTime = DateTime.now() was called right?

2 Likes

Yes, if they want it to constantly update then putting the script in a while loop will automatically update it.

3 Likes

I think its only changing the hours but not the minutes and seconds, how can I fix this?

1 Like

Reference

universalTime.Hour
universalTime.Minute
universalTime.Second
1 Like

Correct me if I’m wrong, but to have the full time in minutes and seconds, you will have to alter this line:

game.Lighting.ClockTime = universalTime.Hour + universalTime.Minute + universalTime.Second
1 Like

What’s the mistake in here?

local dateTime = DateTime.now()
local universalTime = dateTime:ToUniversalTime()
local mezTime = universalTime.Hour + 1
game.Lighting.ClockTime = mezTime + universalTime.Hour + universalTime.Minute + universalTime.Second
1 Like

I do not know if this thread is still at all relevant but I believe the mistake is that you added the universalTime.Hour value twice. Once in the mezTime variable and once when you added them all up.
To fix this, the code would be

local dateTime = DateTime.now()
local universalTime = dateTime:ToUniversalTime()
local mezTime = universalTime.Hour + 1
game.Lighting.ClockTime = mezTime + universalTime.Minute + universalTime.Second
2 Likes