Data saves multiple times unintendedly

I don’t really understand why data saves multiple times by looking at logs here
Thats only happens if the player rejoins a couple of times


Though the player left only one time, why is that happening? What am I doing wrong?

Code:

game.Players.PlayerAdded:connect(function(player)
	local isloaded = Instance.new("BoolValue", player)
	isloaded.Name = "Loaded"
	local statsOpen = Instance.new("BoolValue", player)
	statsOpen.Name = 'statsOpen'
	game.Players.PlayerRemoving:connect(function()
		print('saving user data')
		local save = {}
		
		table.insert(save, player.leaderstats.bobux.Value)
		table.insert(save, player.leaderstats.ribix.Value)
		table.insert(save, player.data.mult.Value)
		table.insert(save, player.data.cooldown.Value)
		table.insert(save, player.data.lvl.Value)
		table.insert(save, player.data.xp.Value)
		table.insert(save, player.data.cooldownCost.Value)
		table.insert(save, player.data.multCost.Value)
		table.insert(save, player.static.coinsCollected.Value)
		table.insert(save, player.static.bigCoinsCollected.Value)
		table.insert(save, player.data.ribixLoop.Value)
		table.insert(save, player.data.convert.Value)
		table.insert(save, player.static.joins.Value)
		
		ds:SetAsync(player.UserId, save)
		print('Data saved')
	end)
end)

The player removing function should be outside of the player added function. Currently, every time a player leaves the player removing function will fire for ever single player in the game.

game.Players.PlayerAdded:connect(function(player)
	local isloaded = Instance.new("BoolValue", player)
	isloaded.Name = "Loaded"
	local statsOpen = Instance.new("BoolValue", player)
	statsOpen.Name = 'statsOpen'
end)

	game.Players.PlayerRemoving:connect(function(player)
		print('saving user data')
		local save = {}
		
		table.insert(save, player.leaderstats.bobux.Value)
		table.insert(save, player.leaderstats.ribix.Value)
		table.insert(save, player.data.mult.Value)
		table.insert(save, player.data.cooldown.Value)
		table.insert(save, player.data.lvl.Value)
		table.insert(save, player.data.xp.Value)
		table.insert(save, player.data.cooldownCost.Value)
		table.insert(save, player.data.multCost.Value)
		table.insert(save, player.static.coinsCollected.Value)
		table.insert(save, player.static.bigCoinsCollected.Value)
		table.insert(save, player.data.ribixLoop.Value)
		table.insert(save, player.data.convert.Value)
		table.insert(save, player.static.joins.Value)
		
		ds:SetAsync(player.UserId, save)
		print('Data saved')
	end)
2 Likes

try to put the function - game.Players.PlayerRemoving:connect(function()
out of this function - game.Players.PlayerAdded:connect(function(player)

game.Players.PlayerAdded:connect(function(player)
         local key = player.UserId
	 local Items
	 local success, response = pcall(function()
		Items = player_data:GetAsync(key) -- These would be the items you already have
	end)
	local isloaded = Instance.new("BoolValue", player)
	isloaded.Name = "Loaded"
	local statsOpen = Instance.new("BoolValue", player)
	statsOpen.Name = 'statsOpen'
end)

	game.Players.PlayerRemoving:connect(function(player)
         local key = player.UserId
		local save = {}
		
		table.insert(save, player.leaderstats.bobux.Value)
		table.insert(save, player.leaderstats.ribix.Value)
		table.insert(save, player.data.mult.Value)
		table.insert(save, player.data.cooldown.Value)
		table.insert(save, player.data.lvl.Value)
		table.insert(save, player.data.xp.Value)
		table.insert(save, player.data.cooldownCost.Value)
		table.insert(save, player.data.multCost.Value)
		table.insert(save, player.static.coinsCollected.Value)
		table.insert(save, player.static.bigCoinsCollected.Value)
		table.insert(save, player.data.ribixLoop.Value)
		table.insert(save, player.data.convert.Value)
		table.insert(save, player.static.joins.Value)
		
	ds:UpdateAsync(key, function(prev)
		return save
	end)
	end)

All in all this would be a better method of saving I would think. (Srry for the bad spacing)

Why are you using UpdateAsync without anything…?

Gurl, just do:

save.bobux = player.leaderstats.bobux.value
And etc. Arrays shouldn’t be used for this kind of thing :wink:

And keep the player removing event out of the player added. That event will fire for everybody like twice or more.