Players have recently been reporting tons of issues with data saving as soon as I added more stats to what I save when the player leaves. To combat this, I introduced an autosave every minute to minimize data loss if it does occur, but I still get reports of data loss of way more than a minute of stats. This seems to happen to certain players multiple times which is very odd. For context here is my autosave code. It runs every minute for the player. I’ve already tried adding trying to save multiple times in case it fails and even if it does fail for the player I don’t get how they can lose more than a couple minutes of data. Any feedback on this is appreciated.
local playerstats = {
["Xp"] = player.leaderstats.Xp.Value,
["HrPoints"] = player.leaderboard.HrPoints.Value,
["ManagerPoints"] = player.leaderstats.ManagerPoints.Value,
["jailStats"] = player.leaderboard.jailtimer.Value,
["banminutes"] = player.leaderboard.BanMinutes.Value,
["IsAfk"] = player.leaderboard.Afk.Value,
["HasWon"] = player.leaderboard.WasAwarded.Value,
["Giftds"] = player.leaderboard.Candy.Value,
["Giveawaydss"] = false,
["spillsCleaned"] = player.leaderboard.spillsCleaned.Value,
["plantsGrown"] = player.leaderboard.plantsGrown.Value,
["minsPlayed"] = player.leaderboard.minsPlayed.Value,
["questType"] = player.leaderboard.questType.Value,
["questReq"] = player.leaderboard.questReq.Value,
["questsComplete"] = player.leaderboard.questsComplete.Value,
["questEnabled"] = player.leaderboard.questEnabled.Value,
["questText"] = player.leaderboard.questText.Value,
["obbiesWon"] = player.leaderboard.obbiesWon.Value,
["potatosWon"] = player.leaderboard.potatosWon.Value,
["minsAfk"] = player.leaderboard.minsAfk.Value,
["capeMins"] = player.leaderboard.capeMins.Value,
["luxurySealMins"] = player.leaderboard.luxurySealMins.Value,
["ordersGiven"] = player.leaderboard.ordersGiven.Value,
["strawberriesGrown"] = player.leaderboard.strawberriesGrown.Value
}
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
playerstatsdatastore:SetAsync(player.UserId, playerstats)
end)
if not success then wait(1) end
until tries == 3 or success
if success then
print("Good job, your datastore works for once.")
end
if not success then
warn("Cannot save data for player!")
end
local Attempts = 1 -- Attempt Counter
end
You might be attempting to save too much data. I doubt it given that the data limits are pretty high (~4mb I believe) but its possible.
What types of data are you saving and on average what is their size? Obviously, things like Xp and HrPoints are probably numbers, but are any of them strings?
Also, try printing out the error. That will definitely help.
Most are integers, a few are booleans, and the questtext is a string. All integers are almost always less than 3 digits long besides xp, which can reach the tens of millions at the high end.
local success, err = false, nil
-- repeat....whatever
success, err = pcall(function()
-- the rest of the code
end)
-- the rest...
if not success then warn(err) end
Also unrelated side note - I would DEFINITELY suggest using attributes since (in my opinion) they are a lot simpler than creating a new NumberValue/IntegerValue/StringValue/BoolValue for everything. Unfortunately they don’t support Instances as values yet but otherwise they are definitely very nice.
(Except for leaderstats - those have to stay as ValueBases)
I am warning the message cannot save data for the player, but when I join the game and check console logs the error never prints. Datastores always work for me in Roblox Studios as well.
I suspected that was part of the issue which is why I implemented an autosave. At most a player should lose one minute of stats, and since the stats save every time a player earns xp they shouldn’t lose any xp. Dataloss is continuing to happen despite this which is really frustrating.
Despite my best efforts I can’t figure out why there is this dataloss still. One pattern I have noticed however is when a player is in a place called the afk room that teleports you to a smaller subplace of the game and then back to the main game they lose data more often. I don’t get how this is logical because not only do I save every 60 seconds, but I also save before they get teleported. How could they be losing many minutes if not over an hour’s worth of xp in this case? I’m about ready to just save xp separately since it’s the most important stat and accept that there will be random data loss for other values.
use profile service for more reliable data store and also use a loop to generate that massive dictionary
--makes the big data table without doing anything manually
local playerstats = {}
for _, valueObject in ipairs(player.leaderboard:GetChildren()) do
playerstats[valueObject.Name] = valueObject.Value
end
That looks a lot neater and means I won’t have to change anything when I add a new stat for quests which is very nice. I’ll have to research profileservice more since making reliable datastores is way too complicated at the moment for me to understand. Do you know if saving so much data to one datastore is going to become a problem? My game currently only has a few concurrent players most of the time, but I am planning a major overhaul and I’d like to be able to at least manage saving data with an average player count of about 20.