Datastore saving tools that were destroyed

When a player sells a tool (destroying it) then leaves, the tool that they supposedly sold is kept with them
How would i fix this?

game.Players.PlayerRemoving:Connect(function(plr)
	
	local toolsOwned = {}
	
	for i, toolInBackpack in pairs(plr.StarterGear:GetChildren()) do
		table.insert(toolsOwned, toolInBackpack.Name)
	end

	local success, errormsg = pcall(function()

		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg)
		print(errormsg)
	end
end)

Did you Destroy the tool from both their Backpack and StarterGear firstly? Cause I don’t see any issue with your code, has to be something related to the destruction of the tool

i use :Destroy() to destroy the tool

Hmm, maybe when you’re leaving, the tool that was supposed to be destroyed is still in StarterGear, maybe add a print(toolInBackpack.Name) in the loop to see if that’s the case, or replace plr.StarterGear:GetChildren() with plr.Backpack:GetChildren()

I have tried both of those and have still come up with nothing

I think the problem is when saving data instead of creating new data it tries to add on to the previous data

It could be, one way to confirm that would be adding a

print(toolsDS:GetAsync(plr.UserId.."-tools"))

Somewhere after setting the new data to see what it prints. And when you tried printing, did it also show the name of the gear that was destroyed?

It did not print the name of the gear that was destroyed

and for some reason it cant print

print(toolsDS:GetAsync(plr.UserId.."-tools"))

Are you deleting these tools from a script, or a local script?

I am deleting them from a server script

Can we see the full script that saves stuff?
And also the one that destroys it?

Probably because the player would leave before that happened.

Nope. It is not. That isn’t possible with the method you’re doing here, unless you’re already getting copies.

What shows up in your player’s backpack and StarterGear can we see?

Tools in startergear

10:09:48.699  RedcliffHorn  -  Server - WeaponDataStore:32
  10:09:48.700  ScrollOfSevenless  -  Server - WeaponDataStore:32
  10:09:48.700  GatlingLaser  -  Server - WeaponDataStore:32
  10:09:48.701  GravitationalRadiationHammerv0.01  -  Server - WeaponDataStore:32
  10:09:48.701  MechatronicFalcon  -  Server - WeaponDataStore:32
  10:09:48.702  PortableJustice  -  Server - WeaponDataStore:32
  10:09:48.702  ForceStaff  -  Server - WeaponDataStore:32
  10:09:48.702  WizardFlySpell  -  Server - WeaponDataStore:32
  10:09:48.703  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.703  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.704  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.704  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.705  StaffOfHealing  -  Server - WeaponDataStore:32
  10:09:48.705  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.706  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.706  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.707  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.707  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.707  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.708  LaserScythe  -  Server - WeaponDataStore:32
  10:09:48.708  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.709  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.709  GrimAxe  -  Server - WeaponDataStore:32
  10:09:48.709  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.710  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.710  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.711  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.711  StaffofNeverendingFrost  -  Server - WeaponDataStore:32
  10:09:48.712  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.712  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.713  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.713  SkeletonScythe  -  Server - WeaponDataStore:32
  10:09:48.714  HealingPotion  -  Server - WeaponDataStore:32
  10:09:48.715  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.715  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.716  StaffofNeverendingFrost  -  Server - WeaponDataStore:32
  10:09:48.716  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.717  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.717  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.717  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.718  DemonSword  -  Server - WeaponDataStore:32
  10:09:48.718  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.719  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.719  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.720  LaserScythe  -  Server - WeaponDataStore:32
  10:09:48.720  SkeletonScythe  -  Server - WeaponDataStore:32

Backpack

10:09:48.699  RedcliffHorn  -  Server - WeaponDataStore:32
  10:09:48.700  ScrollOfSevenless  -  Server - WeaponDataStore:32
  10:09:48.700  GatlingLaser  -  Server - WeaponDataStore:32
  10:09:48.701  GravitationalRadiationHammerv0.01  -  Server - WeaponDataStore:32
  10:09:48.701  MechatronicFalcon  -  Server - WeaponDataStore:32
  10:09:48.702  PortableJustice  -  Server - WeaponDataStore:32
  10:09:48.702  ForceStaff  -  Server - WeaponDataStore:32
  10:09:48.702  WizardFlySpell  -  Server - WeaponDataStore:32
  10:09:48.703  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.703  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.704  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.704  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.705  StaffOfHealing  -  Server - WeaponDataStore:32
  10:09:48.705  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.706  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.706  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.707  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.707  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.707  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.708  LaserScythe  -  Server - WeaponDataStore:32
  10:09:48.708  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.709  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.709  GrimAxe  -  Server - WeaponDataStore:32
  10:09:48.709  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.710  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.710  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.711  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.711  StaffofNeverendingFrost  -  Server - WeaponDataStore:32
  10:09:48.712  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.712  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.713  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.713  SkeletonScythe  -  Server - WeaponDataStore:32
  10:09:48.714  HealingPotion  -  Server - WeaponDataStore:32
  10:09:48.715  FurryTail  -  Server - WeaponDataStore:32
  10:09:48.715  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.716  StaffofNeverendingFrost  -  Server - WeaponDataStore:32
  10:09:48.716  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.717  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.717  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.717  SkullBasher  -  Server - WeaponDataStore:32
  10:09:48.718  DemonSword  -  Server - WeaponDataStore:32
  10:09:48.718  StatueStaffOfStonyJustice  -  Server - WeaponDataStore:32
  10:09:48.719  ElasticaMagica  -  Server - WeaponDataStore:32
  10:09:48.719  BoneSword  -  Server - WeaponDataStore:32
  10:09:48.720  LaserScythe  -  Server - WeaponDataStore:32
  10:09:48.720  SkeletonScythe  -  Server - WeaponDataStore:32

note i am trying to remove steampunk glove from the datastore