Is this an ethical way of storing data?

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?

Any feedback appreciated!

1 Like

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.

1 Like

Have a look in module i made its a game progress saving module. pDataService

Thank you for the suggestion! I looked into it and it should be fairly easy to make!

One question though. How could I go about separating the fresh data from the old ones?

Let’s say there is this data in the leaderboard before refresh:

{ID = 1; Points = 250}
{ID = 2; Points = 500}
{ID = 3; Points = 750}

When the leaderboard refreshes, how should I essentially clear the existing data using this?
Should I make it so the data only lasts 7 days?

In other words, how could I achieve this:

{ID = 1; Points = 0}
{ID = 2; Points = 0}
{ID = 3; Points = 0}

Thanks in advance!

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())
1 Like

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.

Thanks for the suggestion! Although I have an issue…

print(tonumber(DateTime.now():FormatUniversalTime("u", "en-us"))) returns nil (without conversion it returns “u”).

Can you please explain what is the function supposed to return so that I could potentially fix it or work around it? Thanks!

Nevermind, fixed it with os.date()

Code for anyone looking for it:

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.

I thiught u returned the week, im probabky wrong though. Glad you fixed it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.