Delete a tool when equipped - Scripting Help

I made this script to automatically remove a player’s tool when they step in a zone. However, if the player has the tool equipped and they walk into the zone, the tool will stay in their inventory.

How would I be able to remove the player’s tool even when it’s equipped?

Code:

workspace.SwordFightParts.RemoveSword.Touched:Connect(function()
	if plr.Backpack:FindFirstChild("Sword") then
		plr.Backpack.Sword:Destroy()
	end
end)
2 Likes

inside the parameters of (function() put (function(plr) because you have never defined the player

2 Likes

I forgot to put that in the code, but I defined player already.

2 Likes

hmm, is there an error in the output, and can you show me how you defined player, also make sure this is a ServerScript inside the part you want

2 Likes

I put this as a local script in starter player, and I defined player as in:

local plr = game.Players.LocalPlayer
2 Likes

hmm so you want to put it in a normal script… I think? And then when u do delete the player variable and do the (function(plr), lmk if there is errors in output.

also put the serverscript inside the part or in serverscriptservice, if its in the part you can do script.Parent.Touched:Connect(function(plr)

2 Likes

I’m getting an error:

Backpack is not a valid member of Part
2 Likes

If the tool is equipped when the Touched event is fired, the tool would be inside the Character due to it being equipped. To deal with this, you should check if the tool can be found inside the character if not found in the backpack.

The character can be accessed using plr.Character

2 Likes

hm ok so i know what it is… So when you touch the brick, its a body part right, so its searching for the backpack inside the part (ex. lets say the leg touches the part it will search for the backpack inside the leg)

2 Likes

Ah, yes, you need to define the backpack by waiting for the backpack child.

the backpack is the child and the plr is the parent. you have to define the backpack.

2 Likes

so your gonna do something like

part.touched:connect(function(plr)
      if plr.Parent:FindFirstChild("Humanoid") then
      if plr.Parent:FindFirstChild("Yourtool") then
           plr.Parent.YourTool:Destroy()
     end
     end
end)

edit: did what a reply said i think

2 Likes

When the Touched event is fired, the function would return a part, it’s up to you to determine if that part is a body part, and what player that body part belongs to. After you get the player who touched the part, you can run checks to determine if a sword is found in either the backpack or the character, and remove them.

3 Likes

Because tools go into the player’s character when equipped you could check if it’s in their character and delete appropriately

workspace.SwordFightParts.RemoveSword.Touched:Connect(function()
	if plr.Backpack:FindFirstChild("Sword") then
		plr.Backpack.Sword:Destroy()
    elseif plr.Character:FindFirstChild("Sword") then
        plr.Character.Sword:Destroy()
	end
end)

4 Likes

Alright, I fixed everything up and it all works. Thanks for the help!

2 Likes