Making an onClicked script: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Pighead10
Major changes needed, details incorrect and outdated. This still needs further changes but I'm out of time.
Adding ScriptTutorial template, improving code, and using SyntaxHighlight
 
(21 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{CatUp|Tutorials}}
{{CatUp|Tutorials}}
{{ScriptTutorial|easy|scripting}}
__TOC__
__TOC__
== 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 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:
<syntaxhighlight lang="lua">
local function onMouseClick(playerWhoClicked)
    script.Parent.BrickColor = BrickColor.Random()
end


==Introduction==
script.Parent.ClickDetector.MouseClick:connect(onMouseClick)
</syntaxhighlight>
== The Script ==
Here is the first part of the script:
<syntaxhighlight lang="lua">
local function onMouseClick()


This tutorial will show you how to use the a ClickDetector with the scripting event, Clicked. It is commonly referred to as 'onClicked', which is actually incorrect.
end
</syntaxhighlight>
This declares a function called 'onMouseClick'. This does '''NOT MEAN IT HAS TO BE CALLED onMouseClick.''' This could be anything, you're declaring your own function. It could be local function clicked(), or local function brickHasBeenClicked().
<syntaxhighlight lang="lua">
script.Parent.BrickColor = BrickColor.Random()
</syntaxhighlight>


==What is Clicked?==
This tells the game to change the script's parent's - in our case, the brick - BrickColor property to a random color.


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 Clicked scripting event.
<syntaxhighlight lang="lua">
script.Parent.ClickDetector.MouseClick:connect(onMouseClick)
</syntaxhighlight>
This tells the script to (in basic terms) call the function in the parentheses 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 parentheses is 'onMouseClick'.


== Example of How to Use Clicked with a ClickDetector ==
The function in the parentheses 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 ==
In older clients, MouseClick 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: <syntaxhighlight lang="lua" line>
local Players = game:GetService("Players")


1) Insert a brick onto your map.<br />
local distance = 10 -- change this to how far away you can be
2) Insert -> Object -> ClickDetector inside that same brick.<br />
3) Insert a script inside that same brick, but not inside ClickDetector.<br />
4) Double-click the script, and insert the following:<br />


<pre>
while true do
  function onClicked()
wait(1)
  local x = (math.random(0,10) * 0.1)
  local y = (math.random(0,10) * 0.1)
  local z = (math.random(0,10) * 0.1)
  script.Parent.Color = Color3.new (x, y, z)
  end
 
  script.Parent.ClickDetector.MouseClick:connect(onClicked)
</pre>


==The Script==
for i, v in ipairs(Players:GetPlayers()) do
Here is the first part of the scripting:
if (v.Name == script.Parent.Owner.Value) then
local character = v.Character


  function onClicked()
if character then
local torso = character:FindFirstChild(torso, false)


This tells the game to set off the function when the brick is clicked on.
if torso and torso:IsA("Part") and (torso.Position - script.Parent.Position).magnitude < distance then
(This is only an example, so feel free to change this part if you want)
script.Parent.ClickDetector.MaxActivationDistance = distance
 
else
  local x = (math.random(0,10) * 0.1)
script.Parent.ClickDetector.MaxActivationDistance = 0
  local y = (math.random(0,10) * 0.1)
end
  local z = (math.random(0,10) * 0.1)
end
  script.Parent.Color = Color3.new (x, y, z)
end
end
end
</syntaxhighlight>
# In the "MakeScript", paste this: <syntaxhighlight lang="lua" line>
local Players = game:GetService("Players")


This tells the game to change the block's color to a random color.
local distance = 10 -- change this to how far away you can be


Finally, the script is 'connected':
local function onMouseClick()
for i, v in ipairs(Players:GetPlayers()) do
if (v.Name == script.Parent.Owner.Value) then
local character = v.Character


  script.Parent.ClickDetector.MouseClick:connect(onClicked)
if character then
local torso = character:FindFirstChild(torso, false)


==Finding Clicker==
if torso and torso:IsA("Part") and (torso.Position - script.Parent.Position).magnitude < distance then
Even though ClickDetector doesn't have any function arguements so you can't tell who clicked it, it is possible. This is how:
print("Hello world!")  -- this is just an example
===Method 1===
end
This will make it so only one person can click it:
 
*1.Make a part. Name it what you like
*2.Put in 2 Scripts. Name one "CheckScript" and the other "MakeScript"
*3.Insert a StringValue. Name it "Owner". Change the value to who you want to click on it.
*4.Put a ClickDetector into the part, set the MaxActivationDistance to 0
*5.In the "CheckScript", paste this:
 
<pre>
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
 
</pre>
6.In the "MakeScript", paste this:
<pre>
dist = 10 --change this to how far away you can be
 
function onClicked()
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
Line 93: Line 92:
end
end


script.Parent.ClickDetector.MouseClick:connect(onClicked) </pre>
script.Parent.ClickDetector.MouseClick:connect(onMouseClick)
 
</syntaxhighlight>
You can simply replace print("Hello world!") with whatever you want this to do.
You can simply replace print("Hello world!") with whatever you want this to do.
 
=== Method 2 ===
===Method 2===
This method will make it so only a team can click on it.
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: <syntaxhighlight lang="lua" line>
local Players = game:GetService("Players")


1.Make a part. Name it what you like.<br>
local distance = 10 --change this to how far away you can be
2.Put in 2 Scripts. Name one "CheckScript" and the other "MakeScript"<br>
local teamColor = BrickColor.new("Bright yellow") --change this to the team
3.Put a ClickDetector into the part, set the MaxActivationDistance to 0<br>
4.In the "CheckScript", paste this:<br>
<pre>
dist = 10 --change this to how far away you can be
teamcolor = BrickColor.new("Bright yellow") --change this to the team


while true do
while true do
wait(1)
wait(1)
local p = game.Players:GetChildren()
 
for i = 1, #p do
for i, v in ipairs(Players:GetPlayers()) do
if (p[i].TeamColor == teamcolor) then
if (v.TeamColor == teamColor) then
if (game.Workspace:findFirstChild(p[i].Name) ~= nil)
then
if v.Character then
if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist
if (torso.Position - script.Parent.Position).magnitude < distance then  
then  
script.Parent.ClickDetector.MaxActivationDistance = 10
script.Parent.ClickDetector.MaxActivationDistance = 10
else
else
script.Parent.ClickDetector.MaxActivationDistance = 0
script.Parent.ClickDetector.MaxActivationDistance = 0
end
end
end
end
end
end
end
end
end </pre>
end
5.In the "MakeScript", paste this:
</syntaxhighlight>
<pre>
# In the "MakeScript", paste this: <syntaxhighlight lang="lua" line>
dist = 10 --change this to how far away you can be
local distance = 10 -- change this to how far away you can be
teamcolor = BrickColor.new("Bright yellow") --change this to the team
local teamColor = BrickColor.new("Bright yellow") -- change this to the team color


function onClicked()
local function onMouseClick()
for i, v in ipairs(Players:GetPlayers() do
if (v.TeamColor == teamColor) then
local character = v.Character


local p = game.Players:GetChildren()
if character then
for i = 1, #p do
local torso = character:FindFirstChild(torso, false)
if (p[i].TeamColor == teamcolor) then
 
if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist then
if torso and torso:IsA("Part") and (v.Character.Torso.Position - script.Parent.Position).magnitude < distance then
print("Hello world!")
print("Hello world!")
end
end
end
end
end
Line 142: Line 143:
end
end


script.Parent.ClickDetector.MouseClick:connect(onClicked) </pre>
script.Parent.ClickDetector.MouseClick:connect(onMouseClick)
 
</syntaxhighlight>
Change print("Hello world!") to whatever you want.
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.
[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 14:50, 28 April 2023

This is an easy, scripting related tutorial.

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 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

  1. Insert a brick onto your map.
  2. Insert -> Object -> ClickDetector inside that same brick.
  3. Insert a script inside that same brick, but not inside ClickDetector.
  4. Double-click the script, and insert the following:
local function onMouseClick(playerWhoClicked)
    script.Parent.BrickColor = BrickColor.Random()
end

script.Parent.ClickDetector.MouseClick:connect(onMouseClick)

The Script

Here is the first part of the script:

local function onMouseClick()

end

This declares a function called 'onMouseClick'. This does NOT MEAN IT HAS TO BE CALLED onMouseClick. This could be anything, you're declaring your own function. It could be local function clicked(), or local function brickHasBeenClicked().

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.

script.Parent.ClickDetector.MouseClick:connect(onMouseClick)

This tells the script to (in basic terms) call the function in the parentheses 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 parentheses is 'onMouseClick'.

The function in the parentheses 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

In older clients, MouseClick 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:

  1. Make a part. Name it what you like
  2. Put in 2 Scripts. Name one "CheckScript" and the other "MakeScript"
  3. Insert a StringValue. Name it "Owner". Change the value to who you want to click on it.
  4. Put a ClickDetector into the part, set the MaxActivationDistance to 0
  5. In the "CheckScript", paste this:
    local Players = game:GetService("Players")
    
    local distance = 10 -- change this to how far away you can be
    
    while true do
    	wait(1)
    
    	for i, v in ipairs(Players:GetPlayers()) do
    		if (v.Name == script.Parent.Owner.Value) then
    			local character = v.Character
    
    			if character then
    				local torso = character:FindFirstChild(torso, false)
    
    				if torso and torso:IsA("Part") and (torso.Position - script.Parent.Position).magnitude < distance then
    					script.Parent.ClickDetector.MaxActivationDistance = distance
    				else
    					script.Parent.ClickDetector.MaxActivationDistance = 0
    				end
    			end
    		end
    	end
    end
    
  6. In the "MakeScript", paste this:
    local Players = game:GetService("Players")
    
    local distance = 10 -- change this to how far away you can be
    
    local function onMouseClick()
    	for i, v in ipairs(Players:GetPlayers()) do
    		if (v.Name == script.Parent.Owner.Value) then
    			local character = v.Character
    
    			if character then
    				local torso = character:FindFirstChild(torso, false)
    
    				if torso and torso:IsA("Part") and (torso.Position - script.Parent.Position).magnitude < distance then
    					print("Hello world!")  -- this is just an example
    				end
    			end
    		end
    	end 
    end
    
    script.Parent.ClickDetector.MouseClick:connect(onMouseClick)
    

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.

  1. Make a part. Name it what you like.
  2. Put in 2 Scripts. Name one "CheckScript" and the other "MakeScript"
  3. Put a ClickDetector into the part, set the MaxActivationDistance to 0
  4. In the "CheckScript", paste this:
    local Players = game:GetService("Players")
    
    local distance = 10 --change this to how far away you can be
    local teamColor = BrickColor.new("Bright yellow") --change this to the team
    
    while true do
    	wait(1)
    
    	for i, v in ipairs(Players:GetPlayers()) do
    		if (v.TeamColor == teamColor) then
    	
    			if v.Character then
    				if (torso.Position - script.Parent.Position).magnitude < distance then 
    					script.Parent.ClickDetector.MaxActivationDistance = 10
    				else
    					script.Parent.ClickDetector.MaxActivationDistance = 0
    				end
    			end
    		end
    	end
    end
    
  5. In the "MakeScript", paste this:
    local distance = 10 -- change this to how far away you can be
    local teamColor = BrickColor.new("Bright yellow") -- change this to the team color
    
    local function onMouseClick()
    	for i, v in ipairs(Players:GetPlayers() do
    		if (v.TeamColor == teamColor) then
    			local character = v.Character
    
    			if character then
    				local torso = character:FindFirstChild(torso, false)
    
    				if torso and torso:IsA("Part") and (v.Character.Torso.Position - script.Parent.Position).magnitude < distance then
    					print("Hello world!")
    				end
    			end
    		end
    	end 
    end
    
    script.Parent.ClickDetector.MouseClick:connect(onMouseClick)
    

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.