Use click detector with tool or alternative

Hi I want to do a script who when tou click in a scpecific model with a specific tool, it give a virtual ressources I have created.

I just have one problem click detector don’t work with tool, I search in the forum but the solve doesn’t work or isn’t explained clearly

How I can do ?

1 Like

I prefer to use my own sorta ClickDetector system w/ the use of UserInputService.InputBegan & Camera::ScreenPointToRay.

Now you’ll want to Connect on InputBegan firstly, of course this is in a LocalScript:

--// LocalScript, StarterPlayerScripts
local UserInputService = game:GetService("UserInputService");
local Players = game:GetService("Players");

local Player = Players.LocalPlayer; --// Needed to access the LocalPlayer's Character
local Camera = workspace.CurrentCamera;
local ToolName = "Tool"; --// Input your tool name

local function InputBegan(Input, Processed)
     --// Code
end

UserInputService.InputBegan:Connect(InputBegan);

Now inside the function you’ll want to check if the UserInputTypeInput is MouseButton1 (or other UserInputTypes).

Additionally, that it isn’t Processed (meaning that they might be typing in a TextBox or doing some other UI action) and that their character exists with the Tool equipped.

if (Input.UserInputType == Enum.UserInputType.MouseButton1 and not Processed) then
    if (LocalPlayer.Character and LocalPlayer.Character:FindFirstChild(ToolName)) then --// Split into two statements for less longer lines
        --// Code
    end
end

Finally, you’ll want to use ScreenPointToRay w/ the Input.Position however this returned a unit Ray (a Ray which is one in length) and must be extended by firing a longer Ray from the same Origin but with the direction multiplied.

Then you can use FindPartOnRay (or another Ray method) and complete what you’d like to achieve.

local UnitRay = Camera:ScreenPointToRay(Input.Position.X, Input.Position.Y);
local ExtendedRay = Ray.new(UnitRay.Origin, UnitRay.Direction * 5000);
local HitPart = workspace:FindPartOnRay(ExtendedRay, LocalPlayer.Character) --// Ignore the LocalPlayer's Character

if (HitPart) then
    --// Do whatever you'd like with the Part!
end

Tell me how it goes!

4 Likes

Create a Server Script and put it inside of the ClickDetector

local clickDectector = script.Parent

clickDetector.MouseClick:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local tool = player.Character:FindFirstChild("TOOL_NAME_HERE")
    if(tool) then
        --player clicked the model with a certain tool equipped
        --make something happen here
    end)
end)
1 Like

I have an error at the first end the parenthes is underline

Thanks for your answer it’s a bit difficult for me actally i bookmark your answer for later

1 Like
local clickDectector = script.Parent

clickDetector.MouseClick:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local tool = player.Character:FindFirstChild("TOOL_NAME_HERE")
    if tool then
        --player clicked the model with a certain tool equipped
        --make something happen here
    end
end)

Would be a fixation to his code, however this wouldn’t be a good method due to the fact its unreliable. I would recommend using @ReturnedTrue 's method as that way you can place it within the tool and get reliable ray casts. You’d then just bind a click function to check whats currently being hovered over - if its a descendant of any of the button model → do your thing.

ok thanks you for your suggest

Now i have a error : Workspace.Wheat.ClickDetector.Script:3: attempt to index nil with ‘MouseClick’

Whatever:

local clickDectector = script.Parent

Is pointing to must not be a ClickDetector then. Please check it :slight_smile: as it must be a ClickDetector object.

Is just an error of text the variable name is clickDectector anf the event is clickDetector

Yes. It is that, didn’t notice sorry - explains why it returns nil :smile:

I wiil try to understand the first solve because isn’t working

Thanks tou so much is working fine !!!