Datastore sometimes saving data and sometimes not?

I’m trying to make a script that will save leaderstats (coins, wins) + shop items that are bought. Sometimes data doesn’t save and sometimes the data does. This is getting really frusterating. I’ve asked for help on tons of discord servers, but they can’t seem to find any problems

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats

	local coins = Coins.Value

	game.ReplicatedStorage.Coinsgui.OnServerEvent:Connect(function(coins)
		local CashLabel = plr.PlayerGui.Main.Cashlabel.Cash
		while wait() do
			CashLabel.Text = Coins.Value
		end
	end)

	local playerUserId = plr.UserId

	local data
	local success, errormessage = pcall(function()
		data = DataStore1:GetAsync(playerUserId)
	end)

	if data[2] ~= nil then
		for _, toolName in pairs(data[2]) do
			local tool = game.ServerStorage.ShopItems:FindFirstChild(toolName):FindFirstChild(toolName) 
			if tool:IsA("Tool") then
				local NewTool = tool:Clone()
				NewTool.Parent = plr.Backpack
				local NewTool = tool:Clone()
				NewTool.Parent = plr.StarterGear
			else
				local NewTool = tool:Clone()
				NewTool.Parent = plr.Character.Head
			end
		end
	end
	
	local data
	local success, errormessage = pcall(function()
		data = DataStore1:GetAsync(playerUserId)
		print(data)
	end)
	
	if success then
		Coins.Value = data[1].Coins
		Wins.Value = data[1].Wins
	else
		warn(errormessage)
	end


end)

game.Players.PlayerRemoving:Connect(function(plr)
	playerUserId = plr.UserId

	data = {
		Coins = plr.leaderstats.Coins.Value;
		Wins = plr.leaderstats.Wins.Value;
	}

	local toolTables = {}

	for _, tool in pairs(plr.StarterGear:GetChildren()) do
		if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
			table.insert(toolTables, tool.Name)
		end
	end
	
	for _, tool in pairs(plr.Character.Head:GetChildren()) do
		if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
			table.insert(toolTables, tool.Name)
		end
	end
	
	print(toolTables)

	local success, errormessage = pcall(function()
		DataStore1:SetAsync(playerUserId,{data,toolTables})
	end)

	if success then
		print("Successfully saved data!")
		print(DataStore1)
	else
		warn(errormessage)
	end
end)

game:BindToClose(function()
	for _, player in pairs (game.Players:GetPlayers()) do

		data = {
			Coins = player.leaderstats.Coins.Value;
			Wins = player.leaderstats.Wins.Value
		}

		
		local toolTables = {}

		for _, tool in pairs(player.StarterGear:GetChildren()) do
			if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
				table.insert(toolTables, tool.Name)
			end
		end
		
		for _, tool in pairs(player.Character.Head:GetChildren()) do
			if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
				table.insert(toolTables, tool.Name)
			end
		end
		
		print(toolTables)
		
		local success, errormessage = pcall(function()
			DataStore1:SetAsync(playerUserId,{data,toolTables})
		end)

		if success then
			print("Successfully saved data!")
		else
			warn(errormessage)
		end
	end
end)

There is one main problem I see: the data you are wanting to store is inside the Player. This is likely causing the error. If the player instance leaves, the script breaks, thus not saving the data. There is an easy fix, however. Just have a PlrData folder in ReplicatedStorage/ServerStorage, and store the data in there. This will guarantee it will save properly. You will have to change any scripts that reference it, but it will be worth it in the end.

1 Like

Are you talking about the leaderstats or the shop items or Both?

If the player instance is destroyed, or the player’s character, then it won’t work (the script will error). I’m mostly concerned about the plr.Character.Head part, as the character is never a good place to store data. Feel free to ask questions, or private message me if you need help working it out.