Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you activate a function when you click?

Asked by 8 years ago

how do i activate a function on left click?

2 answers

Log in to vote
1
Answered by
iSvenDerp 233 Moderation Voter
8 years ago

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

0
I know I messed it up Ill edited it in just a second iSvenDerp 233 — 8y
0
I edited it:3 Sorry about that iSvenDerp 233 — 8y
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. DeveloperSolo 370 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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!

Answer this question