Need help finding how data losses occur with working datastore code

I’ve put many fixes into the storing, but I still have that rare data loss bug… Can anyone find any flaws in the secondary save script?

dataStore= game:GetService("DataStoreService"):GetDataStore("NarutoHandSignRevolutionBans6")
game.Players.PlayerAdded:connect(function(player)
--initate values
local key = "SECONDSAVE_"..player.userId
local ban = Instance.new("BoolValue", player)
ban.Name = "Banned"

local prestige = Instance.new("IntValue", player)
prestige.Name = "Prestige" 

local res = Instance.new("StringValue", player)
res.Name = "Reason"
--Get Save(if there is one)
local getSave = dataStore:GetAsync(key)
if getSave ~= nil then
	res.Value = getSave.reason
	ban.Value = getSave.ban
	prestige.Value = getSave.prestige
else
	local newSave = {
		ban = false,
		reason = "None",
		prestige = 0
	}
	ban.Value = false
	res.Value = "None"
	prestige.Value = 0
	dataStore:SetAsync(key, newSave)
end
end)
while wait(20) do
	for i, player in pairs(game.Players:GetPlayers()) do
		--save values
		local key = "SECONDSAVE_"..player.userId
		local afterSave = {
				ban = player.Banned.Value,
				reason = player.Reason.Value,
				prestige = player.Prestige.Value
		}
		dataStore:SetAsync(key, afterSave)
		print("I saved"..player.Name.."'s ".." SECONDARY VALUES.")
	end
end
1 Like

Is this the whole script? You dont seem to be saving when a player leaves or the server closes. If you only save on a loop every 20 seconds there is a high chance one of your players may leave before the latest file is saved to the DataStore

Do what @Roytt said about saving when players leave, but you also need to keep in mind that it is not uncommon for a datastore to throw an error from Roblox’s end. When the error occurs the loop is broken then no one else data is saved in that specific server.

The standard practice is to wrap your datastore requests in pcalls which catch runtime errors that you can deal with. Here’s an example.

for _,v in ipairs(game.Players:GetPlayers()) do
    local SaveSuccess = pcall(function() 
        DataStore:SetAsync(v.UserId,{Data});
    end)
    if not SaveSuccess then
        repeat
            local SaveSuccess = pcall(function() 
                  DataStore:SetAsync(v.UserId,{Data});
            end)
            wait(6);
            Count = Count + 1;
         until SaveSuccess or Count > 3
    end)
end

Although many people have already made really good data saving modules, I recommend you just use this one instead and forego the hassle.

2 Likes

Also, saving that fast might cause data to be throttled. (If you are saving after events like trading, loot boxes, etc.)

Ok now the datastore is throttling when I execute the basic script and I’m the only one…

dataStore= game:GetService("DataStoreService"):GetDataStore("NarutoHandSignRevolutionBans6")
local key = "SECONDSAVE_21660878"
		local afterSave = {
				ban = false,
				reason = false,
				prestige = 3
		}
		local Count = 0
		local SaveSuccess = pcall(function() 
	        dataStore:SetAsync(key, afterSave);
	   	end)
	    if not SaveSuccess then
	        repeat
	            local SaveSuccess = pcall(function() 
	                dataStore:SetAsync(key, afterSave);
				end)
				wait(6)
				Count = Count + 1;
			until SaveSuccess or Count > 5
		end
> dataStore= game:GetService("DataStoreService"):GetDataStore("NarutoHandSignRevolutionBans6")
local key = "SECONDSAVE_21660878"
		local afterSave = {
				ban = false,
				reason = false,
				prestige = 3
		}
		local Count = 0
		local SaveSuccess = pcall(function() 
	        dataStore:SetAsync(key, afterSave);
	   	end)
	    if not SaveSuccess then
	        repeat
	            local SaveSuccess = pcall(function() 
	                dataStore:SetAsync(key, afterSave);
				end)
				wait(6)
				Count = Count + 1;
			until SaveSuccess or Count > 5
		end
23:32:56.875 - Request was throttled. Try sending fewer requests. Key = SECONDSAVE_21660878

I think I may have found the reason for the throttle, having multiple save files. One for basic data, codes, bans, and inventory. Four invokes per player.

So put a wait period in between saves?

Yeah, about two minutes for autosaving.