I'm trying to save the data when player leaves. But It doesn't save:

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

	
local data = {player.leaderstats.Cash.Value, player.leaderstats.Level.Value, player.leaderstats.Eggs.Value, player.Current.Value, player.backpackVal.Value}

local success, err = pcall (function()
	game:GetService("DataStoreService"):GetDataStore("SavedData"):SetAsync("playerdata_"..player.UserId, data)
end)
if success then
	
else
	warn(err)
end
	if player:FindFirstChild("PetInventory") then
		local inventory = {}
		for i, v in pairs(player.PetInventory:GetChildren()) do
			table.insert(inventory, v.Name)
		end
		game:GetService("DataStoreService"):GetDataStore("PetsS"):SetAsync(player.UserId.."-pets", inventory)
		if player:FindFirstChild("EquippedPet") then
			if player.EquippedPet.Value ~= nil then
				game:GetService("DataStoreService"):GetDataStore("PetsS"):SetAsync(player.UserId.."-equipped", player.EquippedPet.Value)
			end
		
end
end
end)

Also when I test it in the game alone it saves the first lines (Cash, Level, Eggs, etc.), but doesn't save the pets. Can someone help me?