Real time day cycle

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I would like to achieve a game day/night cycle that is based off the Eastern Time Zone (EST / EDT )

  2. What is the issue?
    The issue is as it is right now it is only doing the hours, however, I would like it to also have the minutes and seconds. I’d also like to display the current time on a GUI but that part is not necessary at this time.

  3. What solutions have you tried so far?
    I have not tried any as I don’t really understand this, I found this code on this post and I tried to understand the code but just can’t grasp it.

I put the code in a while loop so that it updates every 60 seconds / 1 minute however I’m not sure if this is the best way to do it!

while true do	
	local dateTime = DateTime.now()
	local universalTime = dateTime:ToUniversalTime()
	local estTime = universalTime.Hour - 5
	game.Lighting.ClockTime = estTime 
	wait(60)
end

Please help if you can, any is appreciated.

Edit 1:
Changed cstTime to estTime to make the script a little more organized and understandable.

4 Likes

Change this to

universalTime.Hour -= 5
local estTime = DateTime.fromUniversalTime(universalTime)
game.Lighting.ClockTime = estTime:FormatUniversalTime("hh:mm:ss", "en-us")
2 Likes

Okay thanks, anything else that needs to be changed?

1 Like

You could use HttpService to update the time every 60 seconds. Conveniently, there’s this free web API at worldclockapi.com/api/json/est/now which sends back JSON.

2 Likes

Yeah, that could be of interest. Do you know how I could do that? I’m not too good at HttpService.

1 Like

I don’t think there’s a need for HttpService because DateTime is already a thing. I don’t think you need to change anything from what I said before.

3 Likes

For now @myaltaccountsthis’s code works perfectly.

2 Likes

Oop, just found an error in the code you provided.

while true do	
	local dateTime = DateTime.now()
	local universalTime = dateTime:ToUniversalTime()
	universalTime.Hour -= 5
	local estTime = DateTime.fromUniversalTime(universalTime)
	game.Lighting.ClockTime = estTime:FormatUniversalTime("hh:mm:ss", "en-us")
	print(game.Lighting.ClockTime)
	wait(60)
end

ERROR:

ServerScriptService.TimeCycle:5: invalid argument #1 to 'fromUniversalTime' (number expected, got table)
2 Likes

You need to unpack the time information from the universalTime table, as per the arguments for DateTime.fromUniversalTime.

local estTime = DateTime.fromUniversalTime(universalTime.Year, universalTime.Month, universalTime.Day, universalTime.Hour, universalTime.Minute, universalTime.Second, universalTime.Millisecond)

Also, ClockTime is a numeric property, you should be using TimeOfDay if you want to change the time with a string.

game.Lighting.TimeOfDay = estTime:FormatUniversalTime("hh:mm:ss", "en-us")
1 Like

Alright, no errors but by default, it sets to night when it’s almost 3 Pm, is there any solution to this?

This is the current code I have with the edits provided.

while true do	
	local dateTime = DateTime.now()
	local universalTime = dateTime:ToUniversalTime()
	universalTime.Hour -= 5
	local estTime = DateTime.fromUniversalTime(universalTime.Year, universalTime.Month, universalTime.Day, universalTime.Hour, universalTime.Minute, universalTime.Second, universalTime.Millisecond)
	game.Lighting.TimeOfDay = estTime:FormatUniversalTime("hh:mm:ss", "en-us")
	wait(60)
end
2 Likes

I think it should show the correct time if you replace hh:mm:ss with HH:mm:ss, so

game.Lighting.TimeOfDay = estTime:FormatUniversalTime("HH:mm:ss", "en-us")
4 Likes