Is this a safe DataStore?

Hey! Can you please check my code if this is a safe dataStore? It works fine but I want to avoid Data-Loses as much as possible. Thanks.

players.PlayerRemoving:Connect(function(plr)
	--Save his data
	local success, errormessage = pcall(function()
		local leaderstats = plr:WaitForChild("leaderstats")
		local tableToSave = {leaderstats:WaitForChild("Coins").Value, leaderstats:WaitForChild("Gems").Value, leaderstats:WaitForChild("Gifts").Value}
		StatsDataStore:SetAsync(plr.UserId, tableToSave)
	end)
	if errormessage then --Try again
		print(errormessage)
		local leaderstats = plr:WaitForChild("leaderstats")
		local tableToSave = {leaderstats:WaitForChild("Coins").Value, leaderstats:WaitForChild("Gems").Value, leaderstats:WaitForChild("Gifts").Value}
		StatsDataStore:SetAsync(plr.UserId, tableToSave)
	end
end)

game:BindToClose(function()
	for i,v in next, players:GetPlayers() do
		local success, errormessage = pcall(function()
			local leaderstats = v:WaitForChild("leaderstats")
			local tableToSave = {leaderstats:WaitForChild("Coins").Value, leaderstats:WaitForChild("Gems").Value, leaderstats:WaitForChild("Gifts").Value}
			StatsDataStore:SetAsync(v.UserId, tableToSave)
		end)
		if errormessage then
			local leaderstats = v:WaitForChild("leaderstats")
			local tableToSave = {leaderstats:WaitForChild("Coins").Value, leaderstats:WaitForChild("Gems").Value, leaderstats:WaitForChild("Gifts").Value}
			StatsDataStore:SetAsync(v.UserId, tableToSave)
		end
	end
end)

Anything I should improve? I thought about a loop that goes something like this;

while errormessage do
save
end

But idk if that’s a good idea.
Thanks!

Edit: I know, I could use DS2 but I don’t really want to, just tell me if this is safe please.

1 Like

The first thing I would say is use UpdateAsync instead of SetAsync, as it can result in overwrites. Read this thread for more information

Also you could implement your own retry system for the DataStore, I know you do have a retry on Error, but I mean like multiple retries or similar.

Other than that, everything seems good actually.

2 Likes

Try and save every minute, and kick out the player from the game if the data does not load, or prevent them from purchasing the items if the data does not load. BUT ALWAYS CHECK IF THE PLAYERS DATA IS LOADED BEFORE SAVING.
The way I would do it is by inserting a Boolean value into a player, called dataLoaded which will be set to false by default, and set it to true if the data loads without an error message, and have a server script which runs every 60 seconds, with a for loop thru every player. And have it check if the Boolean value is true.
Edit: fixed bad wording

1 Like

Why would you want to kick the player from game if it doesn’t save?

1 Like

Cause to prevent the player from playing the game, and having it not save because the data did not load.

1 Like

I don’t really think thats the way to handle it, because when theres no data it can also mean its a new player right?

Oh, well I said, you kick the player if there is no data AND an error message. Not if there is just no data.