ClickDetector with Tool Equiped

Ok so, i have a simple empty tool, it just prints “Equiped” when selected “Used” when clicked with and “Unequiped” when unselected

I’ve noticed that you can’t click a part that has ClickDetector when the tool is Equiped

Is there a way to click the part when you have the tool equiped?

like:
Tool not equiped output: “I need to take the crowbar to do this”
Tool equiped: “Perfect.”

ClickDetectors don’t work when you have a tool equipped because having the .Activated event and trying to detect if the clickdetector has been clicked is simply not possible. This has happened to me before, where I had to click on a window while holding a hammer tool to break the window. I noticed it didn’t work, came to the forums, and found out why. I had to compromise by adding a press e function to replace the clickdetector. I don’t know any other ways to do it, but just so you know, clicking a TextButton on a surfaceGui doesn’t work, either. I’ve tried.

3 Likes

You really cannot use a ClickDetector while a Tool is equipped - the Tool is responsible for handling clicks in this case.

There are a few different things you can do to get around this.

  1. Make a fake tool. Use no tools at all in the game, so that ClickDetectors always work.
  2. Make the Tool do the actions related to using it. i.e. clicking with ClickDetector will say you need a crowbar in the ClickDetector’s code, but clicking with the Tool would say “Perfect.” in the Tool’s code.
  3. 2, but keeping all the related code in the object that you click

#1 involves just making a custom inventory (or just putting non-Tools in the inventory), then showing all the items in your inventory in a custom GUI.
The hard part is “equipping” the item without putting a Tool in the character that would eat clicks! But you don’t have to do that at all. You can simply get to the point and make the character use the crowbar when they click the detector when there is a crowbar in the inventory.

#2 is the simple and traditional approach. ClickDetector will say “use the crowbar” and have no code that would open the door. Tool will do nothing to anything except the intended object, which it will break, or whatever it’s meant to do in your game.
The problem is that this leaves the code scattered around and makes it hard to maintain, you have to open 2 different and far-apart scripts to change something.

To get #3, you make a unified “use this tool on that” function. If you click the ClickDetector, it will Use your Empty Hand on the thing (and say you need a crowbar). If you click with the crowbar, it will Use your Crowbar on the thing. Then the thing can be the only one with code specific to its own working. You can keep all interactions between items and that thing in one place.
Basically, make tools really do nothing but call a modulescript in the object. And clickdetectors also do nothing but call the exact same modulescript.

1 Like

You could also get the relative mouse position and create a ray cast with the return data and if the ray cast hits the part you can make it do whatever you want.

Usually it is best to fire multiple ray casts from different locations close to the mouse. (e.g. pos = pos - Vector3.new(0.1, 0, 0))

1 Like

Thank you all for the answers, i’ll try those options tomorrow with some scripts.

Referring to Eestlane
I’ve also thinked 2 more options:

  1. The script will just check if the crowbar is in the inventory and when clicked the door an animation will play and the door will open, like you said in the #2

  2. Make a script that will check the distance from the door and when the distance will be like 1-0.5 studs and the player clicks with the tool on the door will open

You could use the Mouse.Target to check if there is a ClickDetector inside the part when the .Activated function is Fired.

6 Likes