Simple Player Syncronised Time of Day with Seasons

In order to syncronise the time between players they must be generating the time bases on a shared value. This usually could be done with some remote events to sync the player as they join but this could de-sync over time, relies on remote events to re-sync and requires communication between scripts.

To avoid this I use os.time(). This should return the same value (seconds since Jan 01 1970. (UTC)) for all players.

First we set our constants; The amount of minutes per day, how many seconds each in game minute shoul be , amount of seasons and a name for each season.

local MINUTES_PER_DAY = 1440 -- amount of minutes in a day ( this should be divisable by 24 hours and 60 minutes)
local GAME_MIN_PER_SECOND = 3 -- how many seconds should pass IRL per in game minute
local SEASONS = 4 -- if you need seasons
local SEASON_NAMES = {"Spring","Summer","Fall","Winter"} -- season names for display

With the constants set we can parse the os.time() using the mod function to get the remaining seconds after dividing by our constants.

while true do
	-- get remaining seconds since Jan 01 1970. (UTC) and use this to get a Time of day and season 
	local tod = math.fmod(os.time(),MINUTES_PER_DAY * GAME_MIN_PER_SECOND) -- give range 0 to 4319 aka each minute of the day * GAME_MIN_PER_SECOND
	local toy = math.fmod(os.time(),MINUTES_PER_DAY * GAME_MIN_PER_SECOND * SEASONS) -- get amount of minutes for all season/year
	toy = math.floor(toy / (MINUTES_PER_DAY * GAME_MIN_PER_SECOND)) -- divide year's minutes by amount of seasons to get the season integer
	tod = tod / GAME_MIN_PER_SECOND -- converts time of day down to 1440(a whole day of minutes) for use by SetMinutesAfterMidnight and clock display

	-- turn minutes into human readable time
	local timeAsString = string.format("%02i:%02i",tod/60%60, tod%60) -- e.g. 14:05
	local seasonAsString = SEASON_NAMES[toy+1]
	
	print("Time is "..timeAsString) -- Time is 08:35
	print("Season is "..seasonAsString) -- Season is Spring
	
	game.Lighting:SetMinutesAfterMidnight(tod) -- set time of day lighting
	wait(1)
end

Now you should be getting a printout each loop with the time and season.

Let me know if you see any way to improve this or simplify it in any way.
Hopefully this helps you get a synched time of day into your game.

6 Likes

Sorry for revival, but I’m just curious on some stuff about this and hope someone can assist!

I want this to work 100% independent from my daylight system, so I’m curious if it will work the same if i just remove “GAME_MIN_PER_SECOND” from my equations?

-Or does anyone know an easier way to determine a cycle of 4 seasons?