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

Function here is being "called" by the last line right?

Asked by 9 years ago

I just want to make sure I have an understanding of it all

function onTouch(part) -- states the function

    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if (humanoid ~= nil) then
        humanoid.Health = 0
    end
end

script.Parent.Touched:connect(onTouch) -- if a person touches the brick then run the function

2 answers

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

You're connecting the function to the Touched event, so every time the brick is touched the function will fire.

1
so "onTouch(part)" means when a part touches it? snoppyploptart 59 — 9y
0
No, that's just what the function is named. You could name it ILIKEPOOP if you wanted. Line 9 is what makes the function fire every time something touches the brick. Perci1 4988 — 9y
0
so the function is "ontouch" but wat is in the parentheses is naming the function? snoppyploptart 59 — 9y
0
What do you mean by "naming" the function? The parentheses contains 1 parameter. If you connect the function to the touched event, that parameter automatically becomes the part that just touched the brick. Perci1 4988 — 9y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The final line,

script.Parent.Touched:connect(onTouch)

is a connection.


You are calling the function connect (indicated by a name + parenthesis: connect()).

When connect is called, it tells ROBLOX to remember the function you gave it (you gave it onTouch).

Then, when something touches the part, ROBLOX will internally then call what you gave the connect method (so it calls onTouch)


The line

function onTouch(part)

means nothing special on its own. You just define a function with one parameter, and set the names of them to what you want.


You could also say

funcToConnect = onTouch;

script.Parent.Touched:connect(funcToConnect);

and it would behave the same as the original.

Answer this question