Making an onClicked script
Introduction
This tutorial will show you how to use the a ClickDetector with the scripting event, MouseClick. It is commonly referred to as 'onMouseClick' or 'onClicked', which are both incorrect.
What is MouseClick?
ROBLOX has released an object called a ClickDetector. If you put it inside a brick, the cursor will change to a hand symbol and it will detect when it has been clicked with the MouseClick scripting event.
Example of How to Use MouseClick with a ClickDetector
- Insert a brick onto your map.
- Insert -> Object -> ClickDetector inside that same brick.
- Insert a script inside that same brick, but not inside ClickDetector.
- Double-click the script, and insert the following:
function onClicked()
script.Parent.BrickColor = BrickColor.Random()
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
The Script
Here is the first part of the script: <syntaxhighlight lang="lua">function onClicked()
This declares a function called 'onClicked'. This does NOT MEAN IT HAS TO BE CALLED onClicked. This could be anything, you're declaring your own function. It could be function clicked(), function brickHasBeenClicked(), or even function LOL_THIS_BRICK_GOT_CLICKED_XD().
<syntaxhighlight lang="lua">script.Parent.BrickColor = BrickColor.Random()
This tells the game to change the script's parent's - in our case, the brick - BrickColor property to a random color.
<syntaxhighlight lang="lua">script.Parent.ClickDetector.MouseClick:connect(onClicked) This tells the script to (in basic terms) call the function in the brackets after the MouseClick event is fired. The MouseClick event fires once the ClickDetector's parent has been clicked on, and in this case the function in the brackets is 'onClicked'.
The function in the brackets after 'connect' needs to be the same as the function name at the beginning: remember that it is activating the function in the brackets. As long as they are both the same, they can be anything you want.
Methods of finding the clicker
Even though Clicked doesn't hold any function arguments that tell you who clicked the brick, it is still possible to discern the most likely person to have clicked. Take note that these don't give you the name of the clicker. These methods make only certain people activate a function after they clicked the brick, based on who was close to the brick at the time. It is not very accurate. If you want an accurate method of finding who clicked bricks, you should use a HopperBin.
Method 1
This will make it so only one person can click it:
- Make a part. Name it what you like
- Put in 2 Scripts. Name one "CheckScript" and the other "MakeScript"
- Insert a StringValue. Name it "Owner". Change the value to who you want to click on it.
- Put a ClickDetector into the part, set the MaxActivationDistance to 0
- In the "CheckScript", paste this:
local dist = 10 --change this to how far away you can be
while true do
wait(1)
local p = game.Players:GetChildren()
for i = 1, #p do
if (p[i].Name == script.Parent.Owner.Value) then
if (game.Workspace:findFirstChild(p[i].Name) ~= nil) then
if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist then
script.Parent.ClickDetector.MaxActivationDistance = dist
else
script.Parent.ClickDetector.MaxActivationDistance = 0
end
end
end
end
end
- In the "MakeScript", paste this:
dist = 10 --change this to how far away you can be
function clicked()
local p = game.Players:GetChildren()
for i = 1, #p do
if (p[i].Name == script.Parent.Owner.Value) then
if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist then
print("Hello world!") -- this is just an example
end
end
end
end
script.Parent.ClickDetector.MouseClick:connect(clicked)
You can simply replace print("Hello world!") with whatever you want this to do.
Method 2
This method will make it so only a team can click on it.
- Make a part. Name it what you like.
- Put in 2 Scripts. Name one "CheckScript" and the other "MakeScript"
- Put a ClickDetector into the part, set the MaxActivationDistance to 0
- In the "CheckScript", paste this:
dist = 10 --change this to how far away you can be
teamcolor = BrickColor.new("Bright yellow") --change this to the team
while true do
wait(1)
local p = game.Players:GetChildren()
for i = 1, #p do
if (p[i].TeamColor == teamcolor) then
if (game.Workspace:findFirstChild(p[i].Name) ~= nil)
then
if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist
then
script.Parent.ClickDetector.MaxActivationDistance = 10
else
script.Parent.ClickDetector.MaxActivationDistance = 0
end
end
end
end
end
5.In the "MakeScript", paste this:
dist = 10 --change this to how far away you can be
teamcolor = BrickColor.new("Bright yellow") --change this to the team
function onClicked()
local p = game.Players:GetChildren()
for i = 1, #p do
if (p[i].TeamColor == teamcolor) then
if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist then
print("Hello world!")
end
end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Change print("Hello world!") to whatever you want.
Help! I don't understand the script!
If you're new to Lua scripting, this can be tricky to understand. The best course of action would be to find some beginner ROBLOX scripting tutorials, or try the beginner scripting tutorials on the wiki.