ClickDetector

Show Deprecated

ClickDetector allows Scripts and LocalScripts to receive pointer input on 3D objects through their MouseClick event. They work when parented to BasePart, Model, or Folder objects. They detect basic mouse events: enter, leave, left click and right click. Touch input on TouchEnabled devices also fires click events.

The default control scripts bind ButtonR2 to interact with ClickDetectors using ContextActionService:BindActivate(), which can also be used to override this. When using gamepads, the center dot triggers MouseHoverEnter and MouseHoverLeave. The bound activation button fires MouseClick.

MaxActivationDistance can be used to limit the distance a player may be from a click detector before it is no longer clickable.

ClickDetector events fire on both the client and the server. Since a LocalScript will only run if it descends from a Player or player Character, it's usually not useful to put a LocalScript inside a ClickDetector, since the script won't run or the object won't be clickable. If you need a LocalScript to detect ClickDetector events, StarterPlayerScripts may be a better place instead.

Input Priority

If multiple ClickDetectors may detect user input, only the deepest will fire events. If two ClickDetectors are siblings, the first will take priority.

If an action bound with ContextActionService uses the same input as a ClickDetector, the action bound with ContextActionService will take priority over the click detector's events.

UserInputService.InputBegan will fire before ClickDetector events.

Code Samples

Place this code inside a Script inside a ClickDetector. The code sample creates a reference to the parent and defines a function to show a message that greets a player. Finally, it connects the MouseClick event to the defined function.

ClickDetector Example

local clickDetector = script.Parent
local function onClicked(player)
-- Show a message to the player
local msg = Instance.new("Message")
msg.Parent = player:FindFirstChild("PlayerGui")
msg.Text = "Hello, " .. player.Name
wait(2.5)
msg:Destroy()
end
-- Connect the function to the MouseClick event
clickDetector.MouseClick:Connect(onClicked)

This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).

Part Anchored Toggle

local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)

Summary

Properties

Events

Properties

CursorIcon

ContentId
Read Parallel

MaxActivationDistance

Read Parallel

Methods

Events

MouseClick

Parameters

playerWhoClicked: Player

MouseHoverEnter

Parameters

playerWhoHovered: Player

Code Samples

Hovering Over and Off a ClickDetector

local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)

MouseHoverLeave

Parameters

playerWhoHovered: Player

Code Samples

Hovering Over and Off a ClickDetector

local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)

RightMouseClick

Parameters

playerWhoClicked: Player