How to remove a tool once the tool touches a part

What I am trying to achieve is a tool gets removed once the tool touches a specific part. The script below detects if a tool named “Box” touches the part (Restock). I tried using v:remove() but it didn’t work.


local Restock = script.Parent

Restock.Touched:Connect(function(Obj)
	if Obj.Parent.Name == "Box" and Obj.Parent:IsA("Tool") then

		print("Part Touched")

		local PLAYER_OBJECT = Players:GetPlayerFromCharacter(Obj.Parent.Parent)

	end
end)
1 Like

You want to make it delete the tool or unequip?

2 Likes

Delete the tool from the player’s inventory

1 Like

Remove is deprecated, use destroy instead.

2 Likes

there’s no difference between remove and destroy, the only difference between them is one is deprecated one is not. something that is deprecated still works, however its preferred to use the not deprecated one.

2 Likes

This should work to achieve what you’re looking for:

local restock = script.Parent

restock.Touched:Connect(function(otherPart)
	local tool = otherPart:FindFirstAncestorWhichIsA("Tool")

	if tool and tool.Name == "Box" then
		tool:Destroy()
	end
end)
1 Like

Here’s another method you can use that works basically the same way as JohhnyLegoKing’s one.

local Restock = script.Parent
Restock.Touched:Connect(function(Obj)
	if Obj.Parent:FindFirstChildWhichIsA("Tool") then
		local Tool = Obj.Parent:FindFirstChildWhichIsA("Tool")
		if Tool.Name == "Box" then
			print("Part Touched")
			Tool:Destroy()
		end
	end
end)
2 Likes

The code works fine for me

local Restock = script.Parent

Restock.Touched:Connect(function(Obj)
	if Obj.Parent.Name == "Box" and Obj.Parent:IsA("Tool") then

		print("Part Touched")

		local PLAYER_OBJECT = game.Players:GetPlayerFromCharacter(Obj.Parent.Parent)
		Obj.Parent:Destroy()

	end
end)
1 Like

it would most likely be better for the player to use a function where one of the player’s parts touches the part and removes the tool, because if you use a function that detects if a tool has touched it, then it will be a smaller hitbox if you know what i mean ofcourse.

1 Like

Modified a bit and it worked. Thanks a lot!

2 Likes

This is true.

You could also use loop function like for,i,v in pairs(your backpack) do

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.