Click to clear certain tool

I am working on a script to place and replace a tool using ClickDetectors, but I do not know how to script a code to destroy certain tools in a player’s inventory on click. Here is my code right now:

place = script.Parent
visible = place.visible
block = game.ServerStorage:WaitForChild'Block'
blockCheck = nil
active = false

place.Block.Part.Touched:Connect(function(part)
if not active then
	active = true
	blockCheck = part.Parent:FindFirstChild("Block")
	if not visible.Value and blockCheck then
		for i,v in pairs(place.Block:GetChildren()) do
			v.Transparency = 0
		end
		blockCheck:Destroy()
		visible.Value = true
	end
	wait(.2)
	active = false
end
end)

place.Block.Part.ClickDetector.MouseClick:Connect(function(player)
if not active then
	active = true
	if visible.Value then
		for i,v in pairs(place.Block:GetChildren()) do
			v.Transparency = 0.8
		end
		local newBlock = block:Clone()
			newBlock.Parent = player.Backpack
			player.Character.Humanoid:EquipTool(newBlock)
		visible.Value = false
	end
	wait(.2)
	active = false
end
end)

Are there any ways to replace the first paragraph with the Touched function with a ClickDetector function without ruining the second part of the script? Can give setup screenshots in needed.

1 Like

You can do something like:

--Make a variables so, for example
-- if it's a script then do
local Tools = {}


And when a player equips tool or they click or touch part, just do

Tools[Player.UserId] = InstanceTool -- or name

And to remove it,

Tools[Player.UserId]:Destroy()

Or let’s suppose those are weapons and there are Primary Secondary, then just do

Tools[Player.UserId.."-Primary"] = InstanceTool

Thanks for the reply, but it doesn’t really help much in my current situation.

What are you trying to do? 30chars

Currently, I have a script that when I collide the tool and the place it will be place, it will remove the tool from my inventory.

I’m trying to change it from Touched:Connect to MouseClick:Connect, so that when I click with the tool in my inventory, it will delete it from my inventory then do the rest of the script.