Could not destroy the tool in Backpack

  1. What do you want to achieve?
    Destroy an item inside of the player’s backpack

  2. What is the issue?
    image


    The tool inside Player’s backpack just couldn’t be destroyed,

  3. What solutions have you tried so far?

local player=game:GetService("Players")
local playerinstance=player:GetPlayerFromCharacter(script.Parent)
local playercharacter=script.Parent
local playerhumanoid=playercharacter:WaitForChild("Humanoid")
local playerbackpack=playerinstance:WaitForChild("Backpack")
playerbackpack.ChildAdded:Connect(function(newchild)
	local newitem=newchild.Name
	local existeditems=playerbackpack:GetChildren()
	for i = 1, #existeditems do
		local items = existeditems[i]
		print(items.Name .. " is item number " .. i)
		if items.Name==newitem or items.ToolTip==newchild.ToolTip then
			local stackamount=newchild:FindFirstChild("Amount")
			local stackenabled=newchild:FindFirstChild("StackingEnabled")
			if stackamount and stackenabled then
				if stackenabled.Value==true then
					stackamount.Value+=1
					newchild:Destroy()
					playerhumanoid:EquipTool(items)
					break
				end
			end	
		end
	end
end)

This script was inside the StarterCharacterScripts
image

1 Like

when the tool destroy do the player equip it?

because when the player equip the tool ,the location of the tool change

No, when the player have his tool in the inventory, I dont think it will be equipped.

yes but while the script is running is the tool equiped or not?

when the tool is equiped his parent is the character

in your script the tool’s parent is the BackPack but if the tool is equiped the parent is the player’s character

No. It is wouldn’t be equipped.

1 Like

but in your video the tool is equiped

ohhhhh Anyways, I will try to fix that out tomorrow, because its currently 11pm

1 Like

heh in my country its 16:15 pm


I made it so that it wouldn’t equip, but the error is still there, somehow, any suggestion to fix this?

1 Like

Do you mind elaborating of what’re you trying to fix?

Maybe because you destroy the newchild?

newchild:Destroy()

When you equip the items of the tool, maybe the newchild is also items?

newchild is an item in the first place since it was cloned into the player’s backpack. newchild:Destroy() is what I am trying to do, but it led to the error I mentioned above.

2 Likes

Maybe

newchild:Destroy()
playerhumanoid:EquipTool(items)

At this code, try to swap those?

This issue occurs because 2 things affect the parent of an object at the same time.
Try to introduce a wait() and see if it fixes the problem. This has at least fixed a lot of problems for me.

1 Like

okay, I will try that out this weekend, thank you!

2 Likes