How to make a tool break an object on touch

Hello! So, I wanted to make a tool that when a player clicks, it plays an animation and checks whether the handle hit a part, and if so, it destroys that part. How would I go about that? I tried making a tool.Activated function but that wouldn’t detect the part touch. Does anyone have any ideas? Thanks!

1 Like

When the tool is activated, connect a touched event to the part. Save the event to a variable.

When the deactivated event fires, disconnect the touched event.

1 Like

So sorry, but I didn’t really understand what you meant by connect an event. Could you please give me an example script? Thanks!

1 Like

part.Touched:Connect(someFunction)

1 Like

Sorry if I didn’t explain well. I know how the touched events work, and how activated events work, but how would I do them together? So like, it only detects the touched event if the activated event has already fired. Thanks!

1 Like
part = script.Parent
tool = script.Parent.Parent
event = nil

function touched()
  --touched code here
end

function activated()
  event = part.Touched:Connect(touched)
end

function deactivated()
  event:Disconnect()
end

tool.Deactivated:Connect(deactivated)
tool.Activated:Connect(activated)
2 Likes

Hello, I will tell you a simple method on how to go about this and provide examples.

Tool Click:
The best thing I like to use when clicking with tools is by using UIS (User Input Service) to check what inputs they pressed along with a bool value checking if the tool is equipped. It should look something like this

local UIS = game:GetService("UserInputService")
local ToolEquipped = false

local TouchedEvent

local function ToolClicked(Input)
      if Input.UserInputType == Enum.UserInputType.MouseButton1Down and ToolEquipped then
         --Play animation
         TouchedEvent = (Handle location).Touched:Connect(function(Hit)
                if Hit.Parent:IsA("BasePart") and Hit.Parent == (whatever you are looking for) then
                     Hit.Parent:Destroy()
                end

                TouchedEvent:Disconnect()
         end)

     end
end
UIS.InputBegan:Connect(ToolClicked)

1 Like

Oh thanks a lot! Let me check if the code works!

It would benefit you to know, that you can save Connected event into variables, and then disconnect those events later.

This is how that is achieved.

1 Like

Thank you!

1 Like

If you have any extra questions, let me know!