Data saves all of my stats into the same integer

hey, I have a problem. my data saves the same number and loads it all into the 3 children of the leaderstats.

my player added script (the end) are already there, as well as connecting it to the functions)

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


function onJoin(player)
	local leaderstats = Instance.new('Folder',player)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue",leaderstats)
	cash.Name = "some cash"
	cash.Value = Data:GetAsync(player.UserId) or 0
	
	local rebirth = Instance.new("IntValue", leaderstats)
	rebirth.Name = "rebirths"
	rebirth.Value = Data:GetAsync(player.UserId) or 0
	
	local punches = Instance.new("IntValue",leaderstats)
	punches.Name = "punches!1!1"
	punches.Value = Data:GetAsync(player.UserId) or 0

here is my player removing script:

function hasLeft(player)
	Data:SetAsync(player.UserId, player.leaderstats["some cash"].Value)
	Data:SetAsync(player.UserId, player.leaderstats.rebirths.Value)
	Data:SetAsync(player.UserId, player.leaderstats["punches!1!1"].Value)
end

game.Players.PlayerRemoving:Connect(hasLeft)

here is a screenshot of my issue:
image

That is because after the each SetAsync, you are overwriting the one prior. I would recommend putting all of the player’s data into a table then saving the table itself. As well, I also recommend using pcalls in the event GetAsync/SetAsync fails.

1 Like