A Mathy Method
@ForeverHD’s Method will work, but you can also utilize os.date
to make your life a bit easier instead of doing a ton of math. os.date
also factors in leap-days, daylight savings and such.
A Easier Method
Basically, what you do is instead of overwriting data, you just save and get it in a different key. That way, you don’t have to wory about past days or years or anything, you can just change the key. Using os.time
and os.date
, we make can make a unique key for the day, month, or year.
To acheive daily leaderboards, simply create a unique key for each day using os.date(), storing the year, month, and day. (year because if the game lasts for a year, old data will be used)
local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"].." M:"..dateTable["month"].." D:"..dateTable["day"]
--Unique key for each day
In case you didn’t see it, My FAQ section has a bullet on this:
How do I make a daily, monthly, or yearly leaderboard?
To make leaderboards “Reset” we can simply change where we save and get the data from. os.time() gives us the number of seconds since the unix equinox. os.date(time) convertes those seconds into a date.
To acheive daily leaderboards, simply create a unique key for each day using os.date()
local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"].." M:"..dateTable["month"].." D:"..dateTable["day"]
--Unique key for each day
To acheive monthly leaderboards, simply create a unique key for each month using os.date()
local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"].." M:"..dateTable["month"]
--Unique key for each month
To acheive yearly leaderboards, simply create a unique key for each year using os.date()
local dateTable = os.date("*t", os.time())
local key = "Y:"..dateTable["year"]
--Unique key for each year