MouseClick
For thread safety, this property is not safe to read in an unsynchronized thread.
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 Player/Character|Character must be within the ClickDetector/MaxActivationDistance|MaxActivationDistance of the clicked object. This event fires when using either a Script or LocalScript.
Platform Support
- On
UserInputService/TouchEnabled|TouchEnabledplatforms, this event fires when the user taps on the same model. - On
UserInputService/GamepadEnabled|GamepadEnabledplatforms, 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
ClickDetector/RightMouseClick|RightMouseClickevent. - 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
ClickDetector/MouseHoverEnter|MouseHoverEnterandClickDetector/MouseHoverLeave|MouseHoverLeaveevents.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
The |
Code Samples
Creating a ClickDetector Door
The code sample below demonstrates the functionality of a ClickDetector and its properties and events.
First the code creates a ClickDetector and set’s its Instance/Parent|parent to the door. It also sets the ClickDetector/MaxActivationDistance|maximum distance players can interact with the ClickDetector to 10 studs and set’s the ClickDetector’s ClickDetector/CursorImage|CursorImage to:
![]()
Then, the code connects the ClickDetector to several events, including changing the door’s BasePart/Transparency when the player’s mouse hovers ClickDetector/MouseHoverEnter|on and ClickDetector/MouseHoverLeave|off off the door. The player opens the door by ClickDetector/MouseClick|left clicking their mouse while over the ClickDetector and closes the door by ClickDetector/RightMouseClick|right clicking their mouse while over the ClickDetector.
This code works in a Script as long as the script’s parent is the door BasePart|Part.
Temporary DataStore
This code sample demonstrates one possible usage of BindableFunction|BindableFunctions, temporary datastores, in combination with ClickDetector|ClickDetectors. It also demonstrates why BindableFunctions are important, since they allow multiple server Script|Scripts to communicate with each other.
Note that this can also be achieved through the use of a ModuleScript.
This example creates a datastore using a table in a Script. Since it does not utilize Roblox’s GlobalDataStore|DataStores, it is temporary and player data is erased when the player leaves or the server shuts down. For persistent DataStores, take a look at this article.
The code below contains two parts:
###TempDataStore Script
The script containing the BindableFunction/OnInvoke callback. Since this is a callback, the Invoke() function will wait for and receive the results of the invoked function. This is the TempDataStore script in the code below.
This script maintains a table called PlayerData that tracks players’ data while they are in the game. When a player enters the game, they are added to the table with a balance of 0 using their Player/UserId|UserId as the table key. When a player exits the game, their key is removed from the table. The script creates two BindableFunctions to interact with the ActivateButton script named GetData and SetData.
When GetData’s OnInvoke event fires, the script returns the player’s balance in PlayerData.
When SetData’s OnInvoke event fires, the script set’s the player’s balance in PlayerData to the value passed as an argument and returns the updated value.
###ActivateButton Script
The script containing the BindableFunction/Invoke function. This is the ActivateButton script in the code below.
It creates a ClickDetector that allows the player to retrieve and increment their balance in the temporary datastore in TempDataStore. When the player left mouse clicks on the parent part the SetData BindableFunction is Invoked, which increases the player’s balance by 1. When the player right mouse clicks on the part the GetData BindableFunction is invoked, which retrieves and prints the player’s balance.