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(),
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
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).