I’m adding a function to my script relating to saving and loading data that adds daily tasks for the user to complete. These daily tasks change every day and rely on temporary datastores to contain data between different play sessions that happen during the same day.
The issue with my code lies in the fact that whenever a player leaves the game, the server prints out a table that correctly includes the “lastResetDate” correctly, although when the player rejoins the game, it’s seemingly gone and the table appears to now only contain the first 3 elements, making it so that when a player leaves the game, and rejoins, all “dailydata” is reset.
What can I do to make it so that the “lastResetDate” data is reloaded (or potentially even saved) successfully so that the “dailydata” resets function correctly?
🔽 Here's the loading part of the script (called when a player joins the game):
if success and data then
print(data) -- prints a table of only the first 3 elements (missing "lastResetDate")
local leaderdata = data[1]
local tooldata = data[2]
local dailydata = data[3]
money.Value = leaderdata[1]
kills.Value = leaderdata[2]
xptot.Value = leaderdata[3]
higheststreak.Value = leaderdata[4]
totalcaptures.Value = leaderdata[5]
totalrounds.Value = leaderdata[6]
swordlevel.Value = tooldata[1]
gunlevel.Value = tooldata[2]
hammerlevel.Value = tooldata[3]
local today = getDateString()
local questData = GlobalData:GetAsync("DailyQuestSet_" .. today)
if not questData then
questData = getRandomQuests()
GlobalData:SetAsync("DailyQuestSet_" .. today, questData)
end
activeDailyQuests[plr.UserId] = questData
QuestsRemote.GetQuests:FireClient(plr, activeDailyQuests)
local lastResetDate = data[4]
local today = getDateString()
-- Check if lastResetDate exists and matches today
if lastResetDate and lastResetDate == today then
dailykills.Value = dailydata[1]
dailystreak.Value = dailydata[2]
dailycaptures.Value = dailydata[3]
dailyupgrades.Value = dailydata[4]
dailyrounds.Value = dailydata[5]
dailybuilds.Value = dailydata[6]
dailywins.Value = dailydata[7]
timeplayed.Value = dailydata[8]
end
-- Recalculate level and XP
local drainedxp = xptot.Value
local function calculatelevels(drainedxp)
local currentlevel = 0
local requiredXP = 145 + (currentlevel - 15) * 3
while drainedxp >= requiredXP do
drainedxp -= requiredXP
currentlevel += 1
requiredXP = 145 + (currentlevel - 15) * 3
end
levels.Value = currentlevel
return drainedxp
end
drainedxp = calculatelevels(drainedxp)
xp.Value = drainedxp
end
💾 And the saving function (called upon a player leaving the game):
function savedata (plr)
local leaderdata = {plr.hiddenstats.Doubloons.Value,
plr.hiddenstats.Kills.Value,
plr.hiddenstats.xptot.Value,
plr.hiddenstats["Highest Streak"].Value,
plr.hiddenstats.totalcaptures.Value,
plr.hiddenstats["Total Rounds"].Value}
local toolData = {plr.toolstats.swordlevel.Value,
plr.toolstats.gunlevel.Value,
plr.toolstats.hammerlevel.Value}
local dailyData = {plr.dailystats.dailykills.Value,
plr.dailystats.dailystreak.Value,
plr.dailystats.dailycaptures.Value,
plr.dailystats.dailyupgrades.Value,
plr.dailystats.dailyrounds.Value,
plr.dailystats.dailybuilds.Value,
plr.dailystats.dailywins.Value,
plr.dailystats.timeplayed.Value}
local lastResetDate = getDateString()
local DataToSave = {leaderdata, toolData, dailyData, lastResetDate}
print(DataToSave) -- ***prints a table of 4 elements, with the correct "lastResetDate"
local success, err = pcall(function()
Data:SetAsync(plr.UserId, DataToSave)
end)
end