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 the ButtonR2 KeyCode to interact with ClickDetectors using ContextActionService:BindActivate(), which can also be used to override this. When using gamepads, the center dot triggers MouseHoverEnter/MouseHoverLeave. The bound activation button fires MouseClick.

Below is a simple template script for working with ClickDetectors. Paste it into a Script or a LocalScript.


local clickDetector = workspace.Part.ClickDetector
function onMouseClick()
print("You clicked me!")
end
clickDetector.MouseClick:Connect(onMouseClick)

MaxActivationDistance can be used to limit the distance a player may be from a ClickDetector object 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's 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 would detect a user input, only the deepest ClickDetector will fire events. If an action bound with ContextActionService uses the same input as a ClickDetector, the action bound with ContextActionService will take priority over ClickDetector events. If two ClickDetectors are siblings, the first ClickDetector will take priority. UserInputService.InputBegan will fire before ClickDetector events. Due to the nature of user input, you ought not depend on all MouseHoverEnter events to fire a matching MouseHoverLeave event.

Code Samples

ClickDetector Example

local clickDetector = script.Parent
local function onClicked(player)
local message = Instance.new("Message")
message.Parent = player:FindFirstChild("PlayerGui")
message.Text = "Hello, " .. player.Name
task.wait(2.5)
message:Destroy()
end
clickDetector.MouseClick:Connect(onClicked)
Part Anchored Toggle

local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local detector = Instance.new("ClickDetector")
detector.Parent = 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()
detector.MouseClick:Connect(onToggle)
Creating a ClickDetector Door

-- The door 'BasePart'
local door = script.Parent
-- Asset ID of the image you want the to set the cursor to
local CursorId = "2287179355"
-- Create a ClickDetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = door
ClickDetector.MaxActivationDistance = 10
ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
-- Make door slightly transparent when mouse hovers over it
ClickDetector.MouseHoverEnter:Connect(function()
door.Transparency = 0.1
end)
-- Door is not transparent when mouse is not hovering over it
ClickDetector.MouseHoverLeave:Connect(function()
door.Transparency = 0
end)
-- Open door on left mouse click
ClickDetector.MouseClick:Connect(function()
door.Transparency = 0.8
door.CanCollide = false
end)
-- Close door on right mouse click
ClickDetector.RightMouseClick:Connect(function()
door.Transparency = 0.1
door.CanCollide = true
end)

Summary

Properties

Sets the mouse icon that will be displayed when the mouse is hovered over this ClickDetector.

Maximum distance between a character and the ClickDetector for the character to be able to click it.

Methods

Events

MouseClick(playerWhoClicked: Player): RBXScriptSignal  

Fires when a player left clicks on the ClickDetector.

MouseHoverEnter(playerWhoHovered: Player): RBXScriptSignal  

Fires when the ClickDetector's parent is hovered upon by a player.

MouseHoverLeave(playerWhoHovered: Player): RBXScriptSignal  

Fires when a player's mouse moves off of the ClickDetector's parent.

RightMouseClick(playerWhoClicked: Player): RBXScriptSignal  

Fires when a player right clicks on the ClickDetector.

Properties

CursorIcon

The CursorIcon sets the mouse icon that will be displayed when the Mouse.mouse hovers over this ClickDetector.

If this property is left blank, the ClickDetector will use the default icon:

Default cursor icon

To change the ClickDetector's cursor icon, set the property to the asset id or URL of the image you would like to use. For instance, setting the property to 2287179377 or this URL changes the cursor icon to:

Custom cursor icon

Code Samples

Creating a ClickDetector Door

-- The door 'BasePart'
local door = script.Parent
-- Asset ID of the image you want the to set the cursor to
local CursorId = "2287179355"
-- Create a ClickDetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = door
ClickDetector.MaxActivationDistance = 10
ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
-- Make door slightly transparent when mouse hovers over it
ClickDetector.MouseHoverEnter:Connect(function()
door.Transparency = 0.1
end)
-- Door is not transparent when mouse is not hovering over it
ClickDetector.MouseHoverLeave:Connect(function()
door.Transparency = 0
end)
-- Open door on left mouse click
ClickDetector.MouseClick:Connect(function()
door.Transparency = 0.8
door.CanCollide = false
end)
-- Close door on right mouse click
ClickDetector.RightMouseClick:Connect(function()
door.Transparency = 0.1
door.CanCollide = true
end)

MaxActivationDistance

The MaxActivationDistance property controls the maximum distance, in studs, between a Character and the ClickDetector for the character to be able to click it. This is used to limit from how far a player can interact with a ClickDetector.

For instance, a character within 10 studs of a ClickDetector with a MaxActivationDistance of 5 would not be able to click the ClickDetector because they are out of range. If, however, the MaxActivationDistance was 15, the character would be able to click it.

Code Samples

Creating a ClickDetector Door

-- The door 'BasePart'
local door = script.Parent
-- Asset ID of the image you want the to set the cursor to
local CursorId = "2287179355"
-- Create a ClickDetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = door
ClickDetector.MaxActivationDistance = 10
ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
-- Make door slightly transparent when mouse hovers over it
ClickDetector.MouseHoverEnter:Connect(function()
door.Transparency = 0.1
end)
-- Door is not transparent when mouse is not hovering over it
ClickDetector.MouseHoverLeave:Connect(function()
door.Transparency = 0
end)
-- Open door on left mouse click
ClickDetector.MouseClick:Connect(function()
door.Transparency = 0.8
door.CanCollide = false
end)
-- Close door on right mouse click
ClickDetector.RightMouseClick:Connect(function()
door.Transparency = 0.1
door.CanCollide = true
end)

Methods

Events

MouseClick

The MouseClick event fires when a player presses and releases the left mouse button while the cursor is hovering over a BasePart or Model with a ClickDetector. Additionally, the Player's Character must be within the MaxActivationDistance of the clicked object. This event fires when using either a Script or LocalScript.

Platform Support

  • On TouchEnabled platforms, this event fires when the user taps on the same model.
  • On GamepadEnabled platforms, this event fires when the center dot is over the same model and the A button is pressed and released.

Related Events

  • If you want to check when a player right clicks on the ClickDetector, you can use the RightMouseClick event.
  • If you want a function to fire when a player hovers on or off of the ClickDetector without mouse clicking it you can use the MouseHoverEnter and MouseHoverLeave events.

Parameters

playerWhoClicked: Player

The Player who clicked on the ClickDetector.


Code Samples

Creating a ClickDetector Door

-- The door 'BasePart'
local door = script.Parent
-- Asset ID of the image you want the to set the cursor to
local CursorId = "2287179355"
-- Create a ClickDetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = door
ClickDetector.MaxActivationDistance = 10
ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
-- Make door slightly transparent when mouse hovers over it
ClickDetector.MouseHoverEnter:Connect(function()
door.Transparency = 0.1
end)
-- Door is not transparent when mouse is not hovering over it
ClickDetector.MouseHoverLeave:Connect(function()
door.Transparency = 0
end)
-- Open door on left mouse click
ClickDetector.MouseClick:Connect(function()
door.Transparency = 0.8
door.CanCollide = false
end)
-- Close door on right mouse click
ClickDetector.RightMouseClick:Connect(function()
door.Transparency = 0.1
door.CanCollide = true
end)

MouseHoverEnter

The MouseHoverEnter event fires when the player's mouse begins hovering over the ClickDetector's parent. This event fires when using either a Script or LocalScript.

Due to the nature of user input, you ought not depend on all MouseHoverEnter events to fire a matching MouseHoverLeave event.

The player does not have to click the ClickDetector for this event to fire. If you want an event to execute when the player clicks, you can use ClickDetector.MouseClick and ClickDetector.RightMouseClick events.

Parameters

playerWhoHovered: Player

The Player whose mouse started hovering on the ClickDetector.


Code Samples

Creating a ClickDetector Door

-- The door 'BasePart'
local door = script.Parent
-- Asset ID of the image you want the to set the cursor to
local CursorId = "2287179355"
-- Create a ClickDetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = door
ClickDetector.MaxActivationDistance = 10
ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
-- Make door slightly transparent when mouse hovers over it
ClickDetector.MouseHoverEnter:Connect(function()
door.Transparency = 0.1
end)
-- Door is not transparent when mouse is not hovering over it
ClickDetector.MouseHoverLeave:Connect(function()
door.Transparency = 0
end)
-- Open door on left mouse click
ClickDetector.MouseClick:Connect(function()
door.Transparency = 0.8
door.CanCollide = false
end)
-- Close door on right mouse click
ClickDetector.RightMouseClick:Connect(function()
door.Transparency = 0.1
door.CanCollide = true
end)
Mouse Hovering On and Off of a ClickDetector

local clickDetector = script.Parent.ClickDetector
clickDetector.MouseHoverLeave:Connect(function(Player)
print("Oh my... " .. Player.Name .. " left the confines of my parent")
end)
clickDetector.MouseHoverEnter:Connect(function(Player)
print("Oh my... " .. Player.Name .. " hovered over my parent")
end)

MouseHoverLeave

The MouseHoverLeave event fires when a player's mouse moves off of the ClickDetector's parent. This event fires when using either a Script or LocalScript.

Due to the nature of user input, you ought not depend on all MouseHoverEnter events to fire a matching MouseHoverLeave event.

The player does not have to click the ClickDetector for this event to fire. If you want an function to run when the player clicks, you can use ClickDetector.MouseClick and ClickDetector.RightMouseClick events.

Parameters

playerWhoHovered: Player

The Player whose mouse stopped hovering on the ClickDetector.


Code Samples

Mouse Hovering On and Off of a ClickDetector

local clickDetector = script.Parent.ClickDetector
clickDetector.MouseHoverLeave:Connect(function(Player)
print("Oh my... " .. Player.Name .. " left the confines of my parent")
end)
clickDetector.MouseHoverEnter:Connect(function(Player)
print("Oh my... " .. Player.Name .. " hovered over my parent")
end)
Creating a ClickDetector Door

-- The door 'BasePart'
local door = script.Parent
-- Asset ID of the image you want the to set the cursor to
local CursorId = "2287179355"
-- Create a ClickDetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = door
ClickDetector.MaxActivationDistance = 10
ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
-- Make door slightly transparent when mouse hovers over it
ClickDetector.MouseHoverEnter:Connect(function()
door.Transparency = 0.1
end)
-- Door is not transparent when mouse is not hovering over it
ClickDetector.MouseHoverLeave:Connect(function()
door.Transparency = 0
end)
-- Open door on left mouse click
ClickDetector.MouseClick:Connect(function()
door.Transparency = 0.8
door.CanCollide = false
end)
-- Close door on right mouse click
ClickDetector.RightMouseClick:Connect(function()
door.Transparency = 0.1
door.CanCollide = true
end)

RightMouseClick

The RightMouseClick event fires when a player presses and releases the right mouse button while the cursor is hovering over a BasePart or Model with a ClickDetector. Additionally, the Player's Character must be within the MaxActivationDistance of the clicked object. This event fires when using either a Script or LocalScript.

Related Events

  • If you want to check when a player left clicks on the ClickDetector, you can use the MouseClick event.
  • If you want a function to fire when a player hovers on or off of the ClickDetector without clicking it you can use the MouseHoverEnter and MouseHoverLeave events.

Parameters

playerWhoClicked: Player

The Player who right mouse clicked on the ClickDetector.


Code Samples

Creating a ClickDetector Door

-- The door 'BasePart'
local door = script.Parent
-- Asset ID of the image you want the to set the cursor to
local CursorId = "2287179355"
-- Create a ClickDetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = door
ClickDetector.MaxActivationDistance = 10
ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
-- Make door slightly transparent when mouse hovers over it
ClickDetector.MouseHoverEnter:Connect(function()
door.Transparency = 0.1
end)
-- Door is not transparent when mouse is not hovering over it
ClickDetector.MouseHoverLeave:Connect(function()
door.Transparency = 0
end)
-- Open door on left mouse click
ClickDetector.MouseClick:Connect(function()
door.Transparency = 0.8
door.CanCollide = false
end)
-- Close door on right mouse click
ClickDetector.RightMouseClick:Connect(function()
door.Transparency = 0.1
door.CanCollide = true
end)