I am currently making a weekly leaderboard for a game of mine, and I don’t exactly know how to “clear” or “reset” the already accumulated data. My idea was to create a new datastore every time the leaderboard should reset, but… I don’t think that’s a good idea to do.
Here’s what I thought of doing:
["WeeklyDonations20241104"],
["WeeklyDonations20241111"], -- A week (7 days) after the previous one
["WeeklyDonations20241118"], -- A week (7 days) after the previous one
["WeeklyDonations20241125"], -- A week (7 days) after the previous one
-- and so on...
I am wondering if I am rightfully worried about this, or if there is any more efficient way of doing this?
I’d recommend looking into Memory Store Sorted Maps, a type of MemoryStore. MemoryStores data expires after a maximum of 45 days which makes them much better than a data store for this situation.
I’d make it so your script calculates how long there is until 11:59 PM on Sunday, or 12:00 AM on Monday, and sets the expiry for that amount of time.
Here’s an example:
local function getTimeUntilWeekEnd(): number
local now = DateTime.now()
local current = now:FormatUniversalTime("u", "en-us")
local daysUntilSunday = 7 - tonumber(current)
local weekEnd = DateTime.fromUniversalTime(now.Year, now.Month, now.Day + daysUntilSunday, 23, 59, 59)
local remaining = weekEnd.UnixTimestampMillis - now.UnixTimestampMillis
return remaining // 1000
end
--example
--also, using SetAsync because we don't need to check previous data at all here
local success, result = pcall(SortedMap.SetAsync, SortedMap, key, points, getTimeUntilWeekEnd())
To manage and reset leaderboard data while retaining player IDs, you can follow this approach:
Store the last reset timestamp in your leaderboard data. When refreshing the leaderboard, check if the current time exceeds the last reset time by a specified interval (like 7 days). If it has, iterate through your leaderboard and reset all player points to zero, then update the last reset timestamp to the current time.
Here’s a quick example of how this can be implemented:
function CheckAndResetLeaderboard(defaultData)
local currentTime = os.time()
local resetInterval = 7 * 24 * 60 * 60 -- 7 days in seconds
if currentTime - defaultData.lastResetTime >= resetInterval then
for _, profile in pairs(ActiveProfiles) do
profile.Points = 0 -- Reset points for each player
end
defaultData.lastResetTime = currentTime -- Update reset time
print("Leaderboard points have been reset.")
end
end
Call this function whenever the leaderboard is refreshed to ensure points are checked and reset as needed. I think this should work that i just said.
local now = os.date("!*t", tick()) -- or DateTime.now(); both return accurate GMT time
local daysUntilSunday = (7 - (now.wday - 1)) % 7
local current = DateTime.fromUniversalTime(now.year, now.month, now.day + daysUntilSunday, 23, 59, 59)
-- The current variable is now equal to Sunday in GMT time.