Datasave not saving when removing coins?

So I have this pretty simple datasave script, but it isnt saving my data upon removing money, even though it saves added money. How do I fix this?

local dss = game:GetService("DataStoreService")

local leaderstatsStore = dss:GetDataStore("leaderstatsStore")

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

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

	local coinsData
	local giftsData
	local success, errormessage = pcall(function()
		coinsData = leaderstatsStore:GetAsync(player.UserId.."-coins")
		giftsData = leaderstatsStore:GetAsync(player.UserId.."-gifts")
	end)

	if success then
		coins.Value = coinsData
		gifts.Value = giftsData
	else
		print("There was an error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		leaderstatsStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
		leaderstatsStore:SetAsync(player.UserId.."-gifts",player.leaderstats.Gifts.Value)
	end)

	if success then
		print("player coins count: "..player.leaderstats.Coins.Value.." player gifts count: "..player.leaderstats.Gifts.Value)
	else
		print("we encountered an error saving your save.")
		warn(errormessage)
	end
end)
1 Like

What do you mean by “upon removing money”?

as in, maybe buying something from a shop.

1 Like

Do you update the value on the server?

yep.

character limit

nvm, I was actually changing the leaderstats on the client. (not saving, changing)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.