How can I remove a tool from the player's backpack?

Hello Developers!
I am struggling on how to remove a tool from the player’s backpack.
I tried doing this piece of code:

rPlayer1.Backpack:FindFirstChild("ClassicSword"):Destroy()

and It attempts to define Destroy but finds nil.
This is because when a tool is equipped, the tool disappears from the backpack and when unequipped, it places it back to the backpack.
How can I fix this?

NOTE: THIS IS A SERVER SCRIPT, NOT A LOCAL SCRIPT!!!

The tool gets parented to the player’s character when equipped, you will need to check the character and if findfirstchildofclass(tool) then destroy(),

What would be the code It has a blue line on the string now.

rPlayer1.Backpack:FindFirstChildOfClass("ClassicSword"):Destroy()

Tools are parented to the player’s character when equipped, then parented to the backpack when it is unequipped.

the trick here is to use FindFirstChild. You would use the object’s name to check if an object can be found from either the player’s backpack or character.

FindFirstChild will return nil when it cannot find the item you are finding for (classic sword), which will be helpful if we use it with an if statement

if backpack:FindFirstChild("ToolName") then -- checks if it exists in backpack

elseif character:FindFirstChild("ToolName") then -- if not in backpack, check if it exist in character

end
1 Like

No, I said you have to look into the player’s character

local Tool = workspace[player.Name]:FindFirstChildOfClass("Tool")

if Tool then
Tool:Destroy()
end
2 Likes

Ok thank you juicy_fruit! Appreciate it!

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Backpack = Player:WaitForChild("Backpack")
		local BackpackTool = Backpack:FindFirstChild("LinkedSword") --Looks for "LinkedSword" tool inside the player's backpack.
		if BackpackTool then
			BackpackTool:Destroy() --Destroys it if found.
		end
		
		local CharacterTool = Character:FindFirstChild("LinkedSword") --Looks for "LinkedSword" tool inside the player's character.
		if CharacterTool then
			CharacterTool:Destroy() --Destroys it if found.
		end
	end)
end)

Here’s how you can check both (check if tool is in the player’s character or if the tool is in the player’s backpack).