Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

4 comments

1

so "onTouch(part)" means when a part touches it?

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.

0

so the function is "ontouch" but wat is in the parentheses is naming the function?

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.