PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

MouseClick

The MouseClick event fires when a player left clicks on the ClickDetector.

If you want to check when a player right clicks on the ClickDetector, you can use the ClickDetector/RightMouseClick function.

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 and ClickDetector/MouseHoverLeave events.

This event functions in Script|Scripts and LocalScript|LocalScripts.

Parameters

Name Type Default Description

The Player who clicked on the ClickDetector


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:

Custom cursor image

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)

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.

-- TempDataStore (Script 1)
local GetData = Instance.new("BindableFunction", game.ServerStorage)
GetData.Name = "GetData"

local SetData = Instance.new("BindableFunction", game.ServerStorage)
SetData.Name = "SetData"

local PlayerData = {}

local function getData(player)
	return PlayerData[player.UserId]
end

local function setData(player, value)
	PlayerData[player.UserId] = value
	return PlayerData[player.UserId]
end

local function addPlayerData(player)
	table.insert(PlayerData, player.UserId, 0)
end

local function removePlayerData(player)
	table.remove(PlayerData, player.UserId)
end

GetData.OnInvoke = getData
SetData.OnInvoke = setData

game.Players.PlayerAdded:Connect(addPlayerData)
game.Players.PlayerRemoving:Connect(removePlayerData)




-- ActivateButton (Script 2)
local GetData = game.ServerStorage:WaitForChild("GetData")
local SetData = game.ServerStorage:WaitForChild("SetData")

local ClickDetector = Instance.new("ClickDetector", script.Parent)

local function getData(player)
	print(player.Name.."'s Current Balance: "..GetData:Invoke(player))
end

local function setData(player)
	local value = 100
	print("Set "..player.Name.."'s Balance to: "..SetData:Invoke())
end

local function incrementData(player)
	local balance = GetData:Invoke(player)
	print("Set "..player.Name.."'s Balance to: "..SetData:Invoke(player, balance+1))
end

ClickDetector.MouseClick:Connect(incrementData)
ClickDetector.RightMouseClick:Connect(getData)