I’ve created a system that uses UNIX time to determine the date in all of my servers within the game, Since UNIX time will be the same on every server, there is no need for complex proccesses to share data between servers.
How it works is that each day is a certain amount of seconds. In my case each day lasts 10
seconds, making each year last an hour and 48 seconds.
For anyone interested in implementing a similar system, here is the source I’ve written that converts UNIX time into totaldays, days of month, month and year, this source also takes into account if the year is a leap year
function isLeapYear(year)
if year % 4 == 0 then
if year % 100 == 0 then
if year % 400 == 0 then
return true
else
return false
end
else
return true
end
else
return false
end
end
local StartingYear = 1
local StartingDay = 1
local StartingMonth = 1
local DaysInMonth =
{
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
}
local ct = os.time
local startct = 1674940684
local SecsPerDay = 10
local StartUp = function()
local oldval = dayindexvalue.Value
dayindexvalue.Value = math.floor((ct()-startct)/SecsPerDay)
dayofmonthvalue.Value += dayindexvalue.Value - oldval
local Month = math.floor(dayindexvalue.Value / 30.427)
local Year = math.floor(Month / 12)
yearindex.Value = Year + 1
monthindexvalue.Value = Month - Year * 12
dayofmonthvalue.Value = dayindexvalue.Value - ((yearindex.Value - 1) * 365.25 + monthindexvalue.Value * 30.427)
end
StartUp()
while true do
task.wait(0.25)
local oldval = dayindexvalue.Value
dayindexvalue.Value = math.floor((ct()-startct)/SecsPerDay)
dayofmonthvalue.Value += dayindexvalue.Value - oldval
local Daysinthismonth = DaysInMonth[monthindexvalue.Value]
if isLeapYear(yearindex.Value + 1) then
DaysInMonth[2] = 29
else
DaysInMonth[2] = 28
end
if dayofmonthvalue.Value >= Daysinthismonth then
dayofmonthvalue.Value = 1
monthindexvalue.Value += 1
end
if monthindexvalue.Value > 12 then
monthindexvalue.Value = 1
dayofmonthvalue.Value = 1
yearindex.Value += 1
end
end
Context:
dayindexvalue:
An int value that contains the total number of days.
monthindexvalue:
An int value containing the index of the month (1 - 12)
yearindex:
An int value containing the current year
dayofmonthvalue:
The day of the current month
startct:
The UNIX time marking the start, Being 1st of January, Year 1
secsperday:
How many seconds each day is represeted by