Clicking Objects while a Tool is Equipped

Time and time again have newer developers dwelled on one of the Tool’s most egregious flaws: its inability of simultaneously using ClickDetectors. After scouring the DevForum desperate for answers, the developer will be greeted with two possibilities—either the misleading “it’s impossible!” or to use a LocalScript resembling the following:

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

tool.Activated:Connect(function()
   if mouse.Target == [your part] then
      --Your code.
   end
end)

This script is okay, the player will be able to click a part and initiate some code all whilst holding a tool. The developer will be happy with their accomplishment and swiftly move on. However, in their fleeting joy, the developer has neglected something. How will the player know what they can actually click? Sure, the developer knows that if the player clicks on an object then an event will trigger. But in contrast, the player will always see this:
image
Alas, we can create a script that does better, a script that will indicate an object is special in some way and entice our player to click on it without altering the object itself. Accomplishing this is rather simple, all we need to do is change the mouse cursor when the player hovers over the desired object.

To do so, we can utilize Mouse.Move and Mouse.Target, changing the cursor back to default when the mouse is no longer hovering over the desired object.

mouse.Move:Connect(function()
	if mouse.Target == [your part] then
		mouse.Icon = "rbxassetid://5992580992" -- Just an example icon.
	else
		mouse.Icon = ""
	end
end)

We may wrap this inside a Tool.Equipped event and call it a day, but again there’s another problem. Even when the tool is unequipped the code will still run, taking up memory and hindering performance. To counter this, we can make a boolean and only run the aforementioned code when the boolean is true.

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
    mouse.Icon = "" -- Set to default in case the player unequips tool while hovering their mouse over the object.
	equipped = false
end)

And so there we have it, our final program:

local player = game.Players.LocalPlayer
local tool = script.Parent

mouse = player:GetMouse()

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

mouse.Move:Connect(function()
	if equipped == true then
		if mouse.Target == [your part] then
			mouse.Icon = "rbxassetid://5992580992"
		else
			mouse.Icon = ""
		end
	end
end)

tool.Activated:Connect(function()
	if mouse.Target == [your part] then
		--Your code.
	end
end)

Thank you for reading and I hope this helps anyone who’s been struggling; please excuse any errors as this is my first formal tutorial. Another thank you to @THEpeoplewhoSEES for co-writing.

9 Likes

this code is very good, i did not try it yet, but i think it will give an error, because you write in the script
mouse.target.Parent
but what if mouse.target is nil?
what if player is pointing the sky?
it will give an error, you must control this one too.


if  mouse.Target then
    -- Your Script
end

1 Like

My mistake, thank you for pointing that out.