Properties
Content
|
Sets the mouse icon that will be displayed when the mouse is hovered over this ClickDetector |
float
|
Maximum distance between a character and the ClickDetector for the character to be able to click it |
bool
|
Determines if an |
string
[ReadOnly]
[NotReplicated]
|
A read-only string representing the class this |
int
[Hidden]
[ReadOnly]
[NotReplicated]
[Deprecated]
|
The cost of saving the instance using data persistence. |
string
|
A non-unique identifier of the |
Instance
[NotReplicated]
|
Determines the hierarchical parent of the |
bool
[Hidden]
|
A deprecated property that used to protect |
bool
[Hidden]
[NotReplicated]
[Deprecated]
|
string
[ReadOnly]
[NotReplicated]
[Deprecated]
|
Functions
void
|
This function destroys all of an |
Instance
|
Create a copy of an object and all its descendants, ignoring objects that are not |
void
|
Sets the |
Instance
|
Returns the first ancestor of the |
Instance
|
Returns the first ancestor of the |
Instance
|
Returns the first ancestor of the |
Instance
|
Returns the first child of the |
Instance
|
Returns the first child of the |
Instance
|
Returns the first child of the |
Instance
|
Variant
|
Returns the attribute which has been assigned to the given name |
RBXScriptSignal
|
Returns an event that fires when the given attribute changes |
Dictionary
|
Returns a dictionary of string → variant pairs for each of the |
Objects
|
Returns an array containing all of the |
string
[NotBrowsable]
|
Returns a coded string of the |
Array
[CustomLuaState]
|
Returns an array containing all of the descendants of the instance |
string
|
Returns a string describing the |
RBXScriptSignal
|
Get an event that fires when a given property of an object changes. |
bool
[CustomLuaState]
|
Returns true if an |
bool
|
Returns true if an |
bool
|
Returns true if an |
void
[Deprecated]
|
Sets the object’s Parent to nil, and does the same for all its descendants. |
void
|
Sets the attribute with the given name to the given value |
Instance
[CustomLuaState]
[CanYield]
|
Returns the child of the |
Objects
[Deprecated]
|
Returns an array of the object’s children. |
Instance
[Deprecated]
|
void
[Deprecated]
|
Instance
[Deprecated]
|
Objects
[Deprecated]
|
bool
[Deprecated]
[CustomLuaState]
|
bool
[Deprecated]
|
void
[Deprecated]
|
Events
RBXScriptSignal
|
Fires when a player left clicks on the ClickDetector |
RBXScriptSignal
|
Fired when the |
RBXScriptSignal
|
Fired when a player’s mouse moves off of the |
RBXScriptSignal
|
Fires when a player right clicks on the ClickDetector |
RBXScriptSignal
[Deprecated]
|
Deprecated in favor of |
RBXScriptSignal
|
Fires when the |
RBXScriptSignal
|
Fires whenever an attribute is changed on the |
RBXScriptSignal
|
Fired immediately after a property of an object changes. |
RBXScriptSignal
|
Fires when an object is parented to this |
RBXScriptSignal
|
Fires when a child is removed from this |
RBXScriptSignal
|
Fires when a descendant is added to the |
RBXScriptSignal
|
Fires immediately before a descendant of the |
RBXScriptSignal
[Deprecated]
|
Code Samples
ClickDetector Example
Place this code inside a Script
inside a ClickDetector
. The code sample first creates a reference to the ClickDetector
parent and defines a function to show a message that greets a player. Finally, it connects the ClickDetector/MouseClick|MouseClick
event of the ClickDetector
to the defined function.
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)
Part Anchored Toggle
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).
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)
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
and a LocalScript
as long as the script’s parent is the door BasePart|Part
.
-- 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)