How can I improve my DataStore?

the saving works totally fine for me, but sometimes I get feedbacks from people with possibly lower ended pcs or on a mobile device (or bad internet connection) saying they’re experiencing data losses or problems with loading the data

serv = game:GetService("DataStoreService")
local Tools = serv:GetDataStore("toolsv1")

LoadData = function(plr,level)

	pcall(function()

		local ActualTools = {}
		local ToolStorage = game.ServerStorage.ITEMS
		if (Tools:GetAsync(plr.UserId)) then
			for _,v in pairs(Tools:GetAsync(plr.UserId)) do
				print(v)
				if ToolStorage:FindFirstChild(v) then
					table.insert(ActualTools, v)
				end
			end
			for _,v in pairs(ActualTools) do
				ToolStorage:FindFirstChild(v):Clone().Parent = plr:WaitForChild("StarterGear")
			end
		end
	end)
end

SaveData = function(plr)

		local ActualTools = {}
		local SG = plr:WaitForChild("StarterGear")
		for _,v in pairs(SG:GetChildren()) do
			table.insert(ActualTools, v.Name)
		end
		if ActualTools then
			Tools:SetAsync(plr.UserId,ActualTools)
			warn("loading tool save")
			wait(5)
		end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr:WaitForChild("Loaded")
	
	local CanAutoSave = true
	
	wait()
	
	pcall(function()
		LoadData(plr)
	end)
		
	
	local AutomaticSaving = true
	
	if AutomaticSaving then
		while wait(60) do
			if (plr~=nil) then
				pcall(function()
					SaveData(plr)
				end)
			else break
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	pcall(function()
			SaveData(plr)
			
	end)
end)

Okay, so i highly recommend using UpdateAsync for saving data. Never use SetAsync for data saving because this method doesn’t take the users old data into account.

Here is an excellent resource describing the ethical responsibility of handling player data:

And here is the documentation of data stores:

Using these resources, player data loss will be a lot less prevalent.

Hopefully this helps!

4 Likes

You should make use of the pcall function to get data save/load failure. As the previous reply, I do suggest using UpdateAsync.

You could also put GetAsync for your tools as a variable in local memory so you don’t call for your data load twice instead of once.

Avoid using wait as a condition on the while loop and just use a while true do loop instead.

Your ActualTools if condition is useless in the data save function because the variable for it is already pre-created so it will always return true, if you’re trying to check if there are contents use #ActualTools which gets you the number of contents then compare it against 0;

if #ActualTools > 0 then

Also not sure what your unused boolean variables are for in the player joined function.