How to click a part when holding a tool?

Hi, I want to use a gun, and click a part with the gun equipped, but it won’t let me, is there anyway around this?

5 Likes

Do you mind explaining more clearly?

2 Likes

You could combine Mouse.Button1Down (roblox.com) and Mouse.Target (roblox.com).

2 Likes

You could get the Player’s mouse using Player:GetMouse() inside a LocalScript, get the Mouse’s Target and check what it hit

2 Likes

I have a empty gun tool, and I want to click a button on a part, but the tool won’t let me click it.
https://gyazo.com/60e434df5f71b392736d16b7686e0130

2 Likes

as much as I know I don’t think there s a property of the tool to avoid this behavior but like many of the comments use mouse.target

2 Likes

Put this inside the Tool, it should be a LocalScript:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent

Tool.Activated:Connect(function()
    local Target = Mouse.Target
    print(Target)
    if Target.Name == "TargetPart" then --It's either that or just "Target"
         print("Found the target >:O")
        --Do stuff here
    end
end)
3 Likes
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent

Tool.Activated:Connect(function()
    print("Clicked!!!")
    local Target = Mouse.Target
    
    if Target:FindFirstChildWhichIsA("ClickDetector") then 
          print("Worked!!!")
        --Do codehere
    end
end)

I changed your code this way he doesn’t have to change every parts name to target part instead he can insert a click detector in the part he wants to click(which i am sure he already did)

2 Likes

Wait I’m just looking at this, you could just check the MouseHoverEnter/MouseHoverLeave Events inside the ClickDetector’s Part if the player is holding a tool or not & if they are you could just change the Mouse’s Icon to the default click Icon maybe :thinking:

2 Likes

This code will loop through the workspace to find any parts with a ClickDetector XP

local mouse = game.Players.LocalPlayer:GetMouse()
local tool = script.Parent

for i, v in pairs(workspace:GetDescendants()) do
tool.Activated:Connect(function()
	local target = mouse.Target
		if v:FindFirstChildWhichIsA("ClickDetector") then
		    if target == v then
				print("Click")
				--do stuff
			end
		end
	end)
end
2 Likes

why is the for loop necessary???

2 Likes

To find everything in the workspace with a ClickDetector

2 Likes

this should work
Under your click detector:

local function MouseClick()
    -- do something
end
shared.ClickDetectors[script.Parent] = MouseClick
script.Parent.MouseClick:Connect(MouseClick)

in a server script:

game.ReplicatedStorage.FireClickDetector.OnServerEvent:Connect(function(player,cd)
     if shared.ClickDetectors[cd] then
            shared.ClickDetectors[cd](player)
     end
end)

in a localscript:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent

Tool.Activated:Connect(function()
    local Target = Mouse.Target

    if Target:FindFirstChildWhichIsA("ClickDetector") then 
           local cd = Target:FindFirstChildWhichIsA("ClickDetector")
           cd.icon = "rbxassetid://2287179377"
           game.ReplicatedStorage.FireClickDetector:FireServer(cd)
    end
end)
2 Likes

but won’t we only need to check if the target has a ClickDetector

2 Likes

is the button a GUI???

1 Like

I’m not entirely sure how they did it as I’ve never tried but I’ve seen games that to enable a tool they somehow have it set to left click. Probably isn’t much help but just thought I’d let you know.

1 Like

I believe it is just a clickdetector

1 Like

then my script will fix your problem

1 Like

It didn’t find the click detector.

try putting a print() in the if statement