Return function if mouse is hovering on click detector?

so basically make my click function not work if local script detects if mouse clicked on a click detector or hovering on a click detector (ill take either)

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if (UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)) then
		--IF STATEMENT OF MOUSE ON CLICK DETECTOR HERE IDK WHAT TO PUT
		--return end
		print("Player Clicked !")
	end
end)

help pls

Just use ClickDetector.MouseClick and ClickDetector.MouseHoverEnter:

workspace.Part.ClickDetector.MouseClick:Connect(function(player)
	print("Clicked")
end)

workspace.Part.ClickDetector.MouseHoverEnter:Connect(function(player)
	print("Hovered")
end)

More information about this is on the documentation of ClickDetector.

yeah but the problem with that is that it detects a specific click detector.

You could create a function of the code that happens when the event is triggered and just call the function when the event is fired Example:

local function this()
	--Code
end

workspace.Part.ClickDetector.MouseClick:Connect(function(player)
	this()
end)

workspace.Part.ClickDetector.MouseHoverEnter:Connect(function(player)
	this()
end)

I mean, you could check to see if the part hovering over is in a table, but this means you won’t need a ClickDetector:

local clickparts = {workspace.Part1, workspace.Part2}
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	if table.find(clickparts, mouse.Target) then
		--do click code
	end
end)

Not exactly what I was looking for but ur advice did give me an idea

local UserInputService = game:GetService("UserInputService")
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if (UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)) then
		local children = Mouse.Target:GetChildren()
		for i, c in pairs(children) do
			if c:IsA("ClickDetector") then
				print("FoudnClickDetector")
				return end
		end
		print("Clicked!")
	end
end)

Thanks

That kind of click is overcomplicated. Instead of that whole thing, you could just do mouse.Button1Down. But, I’m glad you found the solution.

it could but i need input service for my script to work. That was just a raw piece I made from it

should i say MY script. if someone else is doing this use Mouse.Button1Down

1 Like

Mouse.Target can be nil, calling GetChildren() on nil would error so be wary of that.

Instead of iterating over the mouse target’s children it’d be more performant to use FindFirstChild()/FindFirstChildOfClass().

local Target = Mouse.Target
if Target and Target:FindFirstChildOfClass("ClickDetector") then return end
1 Like