Making an onClicked script: Difference between revisions
>Pighead10 Major changes needed, details incorrect and outdated. This still needs further changes but I'm out of time. |
>Brandonhare Fixed up a little bit more stuff. Still not totally fixed. I'll let pighead finish it. |
||
Line 4: | Line 4: | ||
==Introduction== | ==Introduction== | ||
This tutorial will show you how to use the a ClickDetector with the scripting event, | 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 | ==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 | 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 Clicked with a ClickDetector == | == Example of How to Use Clicked with a ClickDetector == | ||
Line 19: | Line 19: | ||
<pre> | <pre> | ||
function onClicked() | function onClicked() | ||
script.Parent.BrickColor = BrickColor.new(Color3.new(math.random(),math.random(),math.random())) | |||
end | end | ||
Line 29: | Line 26: | ||
==The Script== | ==The Script== | ||
Here is the first part of the | Here is the first part of the script: | ||
function onClicked() | function onClicked() | ||
Line 36: | Line 33: | ||
(This is only an example, so feel free to change this part if you want) | (This is only an example, so feel free to change this part if you want) | ||
script.Parent.BrickColor = BrickColor.new(Color3.new(math.random(),math.random(),math.random())) | |||
This tells the game to change the block's color to a random color. | This tells the game to change the block's color to a random color. | ||
Line 59: | Line 53: | ||
<pre> | <pre> | ||
dist = 10 --change this to how far away you can be | local dist = 10 --change this to how far away you can be | ||
while true do | while true do |
Revision as of 11:37, 23 December 2010
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 Clicked 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:
function onClicked() script.Parent.BrickColor = BrickColor.new(Color3.new(math.random(),math.random(),math.random())) end script.Parent.ClickDetector.MouseClick:connect(onClicked)
The Script
Here is the first part of the script:
function onClicked()
This tells the game to set off the function when the brick is clicked on. (This is only an example, so feel free to change this part if you want)
script.Parent.BrickColor = BrickColor.new(Color3.new(math.random(),math.random(),math.random()))
This tells the game to change the block's color to a random color.
Finally, the script is 'connected':
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Finding Clicker
Even though ClickDetector doesn't have any function arguements so you can't tell who clicked it, it is possible. This is how:
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 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
6.In the "MakeScript", paste this:
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 script.Parent.ClickDetector.MouseClick:connect(onClicked)
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:
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.