Reset players data every Monday

Right now I’m working on a daily and weekly quests system. So far the daily quests work and when a quest is completed and the rewards claimed that quest turns a boolean that is saved in the players’ data to true so that the player can’t claim its rewards again

(there is a total of 6 quests available at a time so the players’ data is a small dictionary that looks like this

DailyQuests = {
quest1 = false, 
quest2 = false, 
}
etc

For the daily system, I can have it so that when the quests change, all those saved boolean variables turn back to false since the newly added quests haven’t had their rewards claimed yet.

The issue is I need to do the same for weekly quests and need the players’ quest data to be reset every Monday. But I don’t know if using the same system for the daily quests’ data is possible.

This is the system I use for the daily quest data:

local currentJoinTime = syncedtimeModule.time() --module that gets accurate Unix timestamp
local previousJoinTimeDaily = profile.Data.LastOnlineTimeDaily --the saved unix time stamp of when the player previously left the game

local playerLastOnlineUnixStampDate = DateTime.fromUnixTimestamp(previousJoinTimeDaily):ToUniversalTime()
local formattedPLOUSD = playerLastOnlineUnixStampDate.Month.."/"..playerLastOnlineUnixStampDate.Day.."/"..playerLastOnlineUnixStampDate.Year

local currentTimeStampDate = DateTime.fromUnixTimestamp(currentJoinTime):ToUniversalTime()
local formattedCSD = currentTimeStampDate.Month.."/"..currentTimeStampDate.Day.."/"..currentTimeStampDate.Year

if formattedPLOUSD ~= formattedCSD then
	for _, data in profile.Data.DailyQuests do
		data = false
	end
end

The os.date() function provides a Week Number format which starts on Monday using "%W". If the week number of the current time is different from the week number of the previous time then a Monday has passed.

local currentWeek = tonumber(os.date("%W", currentJoinTime))
local previousWeek = tonumber(os.date("%W", previousJoinTimeDaily))

if currentWeek > previousWeek then
  -- Monday Has Passed
end

Note the DateTime type doesn’t seem to have a Week Number formatting option.

This seems like it would work. Would this cause an issue of the player having their data reset if they join the game multiple times on a Monday?

you can try something like this

local syncedtimeModule = require(game.ServerScriptService.SyncedTime)

local function isMonday(timestamp)
    local date = DateTime.fromUnixTimestamp(timestamp)
    local dayOfWeek = date:FormatLocalTime("dddd", "en-us")
    return dayOfWeek == "Monday"
end

local currentJoinTime = syncedtimeModule.time()
local previousJoinTimeDaily = profile.Data.LastOnlineTimeDaily
local previousJoinTimeWeekly = profile.Data.LastOnlineTimeWeekly or 0

local playerLastOnlineUnixStampDate = DateTime.fromUnixTimestamp(previousJoinTimeDaily):ToUniversalTime()
local formattedPLOUSD = playerLastOnlineUnixStampDate.Month.."/"..playerLastOnlineUnixStampDate.Day.."/"..playerLastOnlineUnixStampDate.Year

local currentTimeStampDate = DateTime.fromUnixTimestamp(currentJoinTime):ToUniversalTime()
local formattedCSD = currentTimeStampDate.Month.."/"..currentTimeStampDate.Day.."/"..currentTimeStampDate.Year

if formattedPLOUSD ~= formattedCSD then
    for _, data in pairs(profile.Data.DailyQuests) do
        data = false
    end
    profile.Data.LastOnlineTimeDaily = currentJoinTime
end

if isMonday(currentJoinTime) and not isMonday(previousJoinTimeWeekly) then
    for _, data in pairs(profile.Data.WeeklyQuests) do
        data = false
    end
    profile.Data.LastOnlineTimeWeekly = currentJoinTime
end