SaveData = function(plr) local ls = plr:FindFirstChild("leaderstats") local saveready = plr:FindFirstChild("SR") if ls ~= nil and saveready ~= nil then if saveready.Value == true then local Coins = ls:FindFirstChild("Coins") local Level = ls:FindFirstChild("Level") local EXP = plr:FindFirstChild("EXP") if Coins ~= nil and Level ~= nil and EXP ~= nil then local tableofstats = { Coins.Value, Level.Value, EXP.Value } local function AttemptSave() local success, msg = pcall(function() GetData = PlayersStats:GetAsync(plr.UserId) end) if success then if GetData then pcall(function() PlayersStats:UpdateAsync(plr.UserId, function() return tableofstats end) end) else pcall(function() PlayersStats:SetAsync(plr.UserId, tableofstats) end) end return true else return false end end local savetest = AttemptSave() if savetest == false then wait(10) local savetest2 = AttemptSave() end end end end end
This is my datastore script, I used to have an autosave function in a while wait(120) loop (every 2 mins) and when the player leaves and I got tons of errors with datastore throttling and data loss reports which was confusing me because when I looked at the budget everything seemed ok. Now I'm only saving when the player leaves but there is still lots of datastore throttling errors and I get reports of data loss. What am I doing wrong? Thanks
You are probably calling this function way too often, and every time you call it (no matter how often or at what frequency), it will definitely try to save once, and if it fails the first time, it will try to save twice.
That is why you might want to add the code that calls the SaveData
function, and then also add everything related to that caller code, e.g.:
SaveData
is called by code that is not properly debounced, e.g. from a Touched
event handler?And so on...
The key: Make sure, you don't try to call any code that loads or saves data too many times.