How do I detect if a player is holding left click on a part?

I’m currently making a game where the player needs to hold down on a button to activate a light but from what I can tell you can’t detect if a player is holding down on a click detector. How would I go about detecting if a player was holding down on a part?

1 Like

you don’t always have to use click detectors.

Create a local script and put it in StarterPlayerScripts

local mouse = game.Players.LocalPlayer:GetMouse() --get the mouse
local part = [your part] --put your part here

mouse.Button1Down:Connect(function() --whenever the left mouse button is pressed down
	if mouse.Target == part then --check if what the mouse is hovering is the part you want
		--rest of code here
	end
end)

1 Like

Unfortunately Roblox does not allow ClickDetectors to work while players are holding an item. To fix that, you can just create a new Local Script with this following code:

tool.Activated:Connect(function() --listening to when a player clicks (while holding the item)
   if not tool.Equipped then return end --just making sure the tool is indeed equipped
   
   if mouse.Target and mouse.Target:FindFirstChild("[The name of your clickDetector]") then --checking if the mouse is clicking on the part with the click detector
   	if (tool.Handle.Position - mouse.Target.Position).Magnitude <= mouse.Target.ClickDetector.MaxActivationDistance then --making sure the player is in the range of the click detector
   		--the code that you want to run here
   	end
   end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.