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.


1local clickDetector = workspace.Part.ClickDetector
2
3function onMouseClick()
4 print("You clicked me!")
5end
6
7clickDetector.MouseClick:Connect(onMouseClick)
8

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

1local clickDetector = script.Parent
2
3local function onClicked(player)
4 local message = Instance.new("Message")
5 message.Parent = player:FindFirstChild("PlayerGui")
6 message.Text = "Hello, " .. player.Name
7 task.wait(2.5)
8 message:Destroy()
9end
10
11clickDetector.MouseClick:Connect(onClicked)
Creating a ClickDetector Door

1-- The door 'BasePart'
2local door = script.Parent
3
4-- Asset ID of the image you want the to set the cursor to
5local CursorId = "2287179355"
6
7-- Create a ClickDetector
8local ClickDetector = Instance.new("ClickDetector")
9ClickDetector.Parent = door
10ClickDetector.MaxActivationDistance = 10
11ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
12
13-- Make door slightly transparent when mouse hovers over it
14ClickDetector.MouseHoverEnter:Connect(function()
15 door.Transparency = 0.1
16end)
17
18-- Door is not transparent when mouse is not hovering over it
19ClickDetector.MouseHoverLeave:Connect(function()
20 door.Transparency = 0
21end)
22
23-- Open door on left mouse click
24ClickDetector.MouseClick:Connect(function()
25 door.Transparency = 0.8
26 door.CanCollide = false
27end)
28
29-- Close door on right mouse click
30ClickDetector.RightMouseClick:Connect(function()
31 door.Transparency = 0.1
32 door.CanCollide = true
33end)
Part Anchored Toggle

1local part = script.Parent
2
3-- Create a ClickDetector so we can tell when the part is clicked
4local detector = Instance.new("ClickDetector")
5detector.Parent = part
6
7-- This function updates how the part looks based on its Anchored state
8local function updateVisuals()
9 if part.Anchored then
10 -- When the part is anchored...
11 part.BrickColor = BrickColor.new("Bright red")
12 part.Material = Enum.Material.DiamondPlate
13 else
14 -- When the part is unanchored...
15 part.BrickColor = BrickColor.new("Bright yellow")
16 part.Material = Enum.Material.Wood
17 end
18end
19
20local function onToggle()
21 -- Toggle the anchored property
22 part.Anchored = not part.Anchored
23
24 -- Update visual state of the brick
25 updateVisuals()
26end
27
28-- Update, then start listening for clicks
29updateVisuals()
30detector.MouseClick:Connect(onToggle)

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.

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.

Methods

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

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.

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

Temporary DataStore

1local Players = game:GetService("Players")
2local ServerStorage = game:GetService("ServerStorage")
3
4-- TempDataStore (Script 1)
5local GetData = Instance.new("BindableFunction")
6GetData.Name = "GetData"
7GetData.Parent = ServerStorage
8
9local SetData = Instance.new("BindableFunction")
10SetData.Name = "SetData"
11SetData.Parent = ServerStorage
12
13local PlayerData = {}
14
15local function getData(player)
16 return PlayerData[player.UserId]
17end
18
19local function setData(player, value)
20 PlayerData[player.UserId] = value
21 return PlayerData[player.UserId]
22end
23
24local function addPlayerData(player)
25 table.insert(PlayerData, player.UserId, 0)
26end
27
28local function removePlayerData(player)
29 table.remove(PlayerData, player.UserId)
30end
31
32GetData.OnInvoke = getData
33SetData.OnInvoke = setData
34
35Players.PlayerAdded:Connect(addPlayerData)
36Players.PlayerRemoving:Connect(removePlayerData)
37
38-- ActivateButton (Script 2)
39local ClickDetector = Instance.new("ClickDetector")
40ClickDetector.Parent = script.Parent
41
42local function getDataClickDetector(player)
43 print(player.Name .. "'s Current Balance: " .. GetData:Invoke(player))
44end
45
46local function setDataClickDetector(player, value)
47 value = value or 100
48 print("Set " .. player.Name .. "'s Balance to: " .. SetData:Invoke(value))
49end
50
51local function incrementDataClickDetector(player)
52 local balance = GetData:Invoke(player)
53 print("Set " .. player.Name .. "'s Balance to: " .. SetData:Invoke(player, balance + 1))
54end
55
56ClickDetector.MouseClick:Connect(incrementDataClickDetector)
57ClickDetector.RightMouseClick:Connect(setDataClickDetector)
58ClickDetector.MouseHoverEnter:Connect(getDataClickDetector)
Creating a ClickDetector Door

1-- The door 'BasePart'
2local door = script.Parent
3
4-- Asset ID of the image you want the to set the cursor to
5local CursorId = "2287179355"
6
7-- Create a ClickDetector
8local ClickDetector = Instance.new("ClickDetector")
9ClickDetector.Parent = door
10ClickDetector.MaxActivationDistance = 10
11ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
12
13-- Make door slightly transparent when mouse hovers over it
14ClickDetector.MouseHoverEnter:Connect(function()
15 door.Transparency = 0.1
16end)
17
18-- Door is not transparent when mouse is not hovering over it
19ClickDetector.MouseHoverLeave:Connect(function()
20 door.Transparency = 0
21end)
22
23-- Open door on left mouse click
24ClickDetector.MouseClick:Connect(function()
25 door.Transparency = 0.8
26 door.CanCollide = false
27end)
28
29-- Close door on right mouse click
30ClickDetector.RightMouseClick:Connect(function()
31 door.Transparency = 0.1
32 door.CanCollide = true
33end)

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

1-- The door 'BasePart'
2local door = script.Parent
3
4-- Asset ID of the image you want the to set the cursor to
5local CursorId = "2287179355"
6
7-- Create a ClickDetector
8local ClickDetector = Instance.new("ClickDetector")
9ClickDetector.Parent = door
10ClickDetector.MaxActivationDistance = 10
11ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
12
13-- Make door slightly transparent when mouse hovers over it
14ClickDetector.MouseHoverEnter:Connect(function()
15 door.Transparency = 0.1
16end)
17
18-- Door is not transparent when mouse is not hovering over it
19ClickDetector.MouseHoverLeave:Connect(function()
20 door.Transparency = 0
21end)
22
23-- Open door on left mouse click
24ClickDetector.MouseClick:Connect(function()
25 door.Transparency = 0.8
26 door.CanCollide = false
27end)
28
29-- Close door on right mouse click
30ClickDetector.RightMouseClick:Connect(function()
31 door.Transparency = 0.1
32 door.CanCollide = true
33end)
Mouse Hovering On and Off of a ClickDetector

1local clickDetector = script.Parent.ClickDetector
2
3clickDetector.MouseHoverLeave:Connect(function(Player)
4 print("Oh my... " .. Player.Name .. " left the confines of my parent")
5end)
6
7clickDetector.MouseHoverEnter:Connect(function(Player)
8 print("Oh my... " .. Player.Name .. " hovered over my parent")
9end)

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

Creating a ClickDetector Door

1-- The door 'BasePart'
2local door = script.Parent
3
4-- Asset ID of the image you want the to set the cursor to
5local CursorId = "2287179355"
6
7-- Create a ClickDetector
8local ClickDetector = Instance.new("ClickDetector")
9ClickDetector.Parent = door
10ClickDetector.MaxActivationDistance = 10
11ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
12
13-- Make door slightly transparent when mouse hovers over it
14ClickDetector.MouseHoverEnter:Connect(function()
15 door.Transparency = 0.1
16end)
17
18-- Door is not transparent when mouse is not hovering over it
19ClickDetector.MouseHoverLeave:Connect(function()
20 door.Transparency = 0
21end)
22
23-- Open door on left mouse click
24ClickDetector.MouseClick:Connect(function()
25 door.Transparency = 0.8
26 door.CanCollide = false
27end)
28
29-- Close door on right mouse click
30ClickDetector.RightMouseClick:Connect(function()
31 door.Transparency = 0.1
32 door.CanCollide = true
33end)
Mouse Hovering On and Off of a ClickDetector

1local clickDetector = script.Parent.ClickDetector
2
3clickDetector.MouseHoverLeave:Connect(function(Player)
4 print("Oh my... " .. Player.Name .. " left the confines of my parent")
5end)
6
7clickDetector.MouseHoverEnter:Connect(function(Player)
8 print("Oh my... " .. Player.Name .. " hovered over my parent")
9end)

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

1-- The door 'BasePart'
2local door = script.Parent
3
4-- Asset ID of the image you want the to set the cursor to
5local CursorId = "2287179355"
6
7-- Create a ClickDetector
8local ClickDetector = Instance.new("ClickDetector")
9ClickDetector.Parent = door
10ClickDetector.MaxActivationDistance = 10
11ClickDetector.CursorIcon = "rbxassetid://" .. CursorId
12
13-- Make door slightly transparent when mouse hovers over it
14ClickDetector.MouseHoverEnter:Connect(function()
15 door.Transparency = 0.1
16end)
17
18-- Door is not transparent when mouse is not hovering over it
19ClickDetector.MouseHoverLeave:Connect(function()
20 door.Transparency = 0
21end)
22
23-- Open door on left mouse click
24ClickDetector.MouseClick:Connect(function()
25 door.Transparency = 0.8
26 door.CanCollide = false
27end)
28
29-- Close door on right mouse click
30ClickDetector.RightMouseClick:Connect(function()
31 door.Transparency = 0.1
32 door.CanCollide = true
33end)

Methods