how do i activate a function on left click?
✓ ANSWER ACCEPTED
How do you activate a function when you click?
2 answers
✓ Accepted answer
Hi...I will show you an example. This destroys a part once you click it.
function onMouseClick() --Like you asked this is calling the function.
script.Parent:Destroy()--Put this script in a part
end
script.Parent.ClickDetector.MouseClick:connect(onMouseClick)--this connects the function so it will fire.
Hope this helps:) You also need to add a click detector and put it in the part. Feel free to accept this answer if it helped:)
3 comments
0
I know I messed it up Ill edited it in just a second
0
I edited it:3 Sorry about that
0
That does work, but doing 'script.Parent.ClickDetector.MouseClick:connect(onMouseClick)' is not the best way and takes more time. Just my opinion. You should learn anonymous functions to save time.
Here you go!
--This little bit makes sure you have the clickdetector in it, if you already did it, remove it.
local x = Instance.new("ClickDetector")
x.Parent = game.Workspace.Part --whatever the name of the brick
x.MaxActivationDistance = 32 --Incase if you want people to be closer or farther away from it
--Actual code
script.Parent.ClickDetector.MouseClick:connect(function() -- a better way than what the person above me did. faster and saves time.
script.Parent.BrickColor = BrickColor.new(math.random(), math.random(), math.random()) -- this just changes it to a random color.
end)
If you like it, make sure you accept this answer to help me and you out!
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
http://wiki.roblox.com/index.php?title=API:Class/Mouse