Tool remaining in my inventory even after being :Destroy()'ed despite the prints claiming tool.Parent is nil

edit: it was an issue with the gun holsters, since the accessory and tool were the exact same name it sometimes deleted the accessory rather than the tool and let the player keep their gun

  1. What do you want to achieve? Keep it simple and clear!
    I want the upgrade shop to briefly unequip the tool before deleting it, so it can do things like cancel animations or remove any speed buffs before the tool gets deleted, so it doesn’t leave anything behind

  2. What is the issue? Include screenshots / videos if possible!
    Despite calling :Destroy() on the item, it still exists just fine in my inventory. I tried doing print(item.Parent), and returns nil despite it existing fine in my inventory. This allows the player to keep the item they just upgraded while still recieving the new one

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    waiting for a bit before deleting it in case humanoid:UnequipTools() is somehow pulling it out of the void, it didn’t work

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Part where it gets deleted

if player.Backpack:FindFirstChild(button.ItemToRemove.Value.Name) then
	clearConnectedAccessories(player.Backpack[button.ItemToRemove.Value.Name], player.Character)
	player.Backpack[button.ItemToRemove.Value.Name]:Destroy()
else
	local item = player.Character:FindFirstChild(button.ItemToRemove.Value.Name)
	print(item)
	
	if item ~= nil then
		clearConnectedAccessories(item, player.Character)
		safeDeleteItem(item, player.Character)
	else
		warn("could not find the instanced item to remove.")
	end
end


Functions

local function safeDeleteItem(item, char)
	if char:FindFirstChild("Humanoid") ~= nil then
		char.Humanoid:UnequipTools()
		task.wait(0.01)
		print(item.Name .. " destroyed")
		print(item.Parent)
		item:Destroy()
		print(item.Parent)
		task.wait(0.5)
		print(item.Parent)
		print(player.Backpack:GetChildren())
	end
end

-- I don't think this second one should be relevant to the issue but I've left it for clarity
local function clearConnectedAccessories(item, char)
	if char == nil then		return		end
	
	for index, accessory in ipairs(char:GetChildren()) do
		if accessory:IsA("Accessory") then
			local toolAttachValue = accessory:FindFirstChild("ToolAttachValue")
			
			if toolAttachValue ~= nil then
				
				if toolAttachValue.Value == item then
					accessory:Destroy()
				end
				
			end
		end
	end
end

Example with trying to upgrade the DP2A1:

The fact that item.Parent is nil yet backpack:GetChildren() still shows the item indicates that there is a second item. Can you verify if there’s a duplicated item in the backpack somehow? Is it perhaps getting added back after the first one is destroyed?

You can check if they’re the same with something like this at the end of safeDeleteItem

for _, child in player.Backpack:GetChildren() do
    if child.Name == item.Name then
        print(if child == item then "Item still exists" else "Duplicate tool found")
    end
end

It seems impossible to me for “Item still exists” to ever print, since you already verified item.Parent is nil. So I expect “Duplicate tool found” to print.

It was an issue with the holstered gun system I recently added, the accessory names matched the tool name exactly so it deleted the accessory instead of the target gun

after :Destroy() is called on an instance, the tool .Parent is set to nil and the property is locked, if you have any references to the tool, they will still live in memory and can read properties as you would normally. the issue in your code is likely item not being parent of the char, means you are destroying the tool from other character and checking completely another one (which still has the tool)
consider passing a name of the tool and then finding of a tool in the player with that name, then destroying