Alright I'm reading a tutorial on arguments and we start off with these simple lines of code:
function touch() script.Parent:remove() end script.Parent.Touched:connect(touch)
and then we switch to more complicated lines of code.
function touch(hit) hit:remove() end script.Parent.Touched:connect(touch)
Now mainly I'm wondering why lines of code number 2 removes your legs. Like can someone please make a story and put ourselves into the shoes of the script. Like I don't understand why our legs refer to hit, kind of thing. Please explain if confused please tell me what your confused about my question.
The function definition includes an argument:
function touch(hit)
That hit
is an argument. It's a value that the function gets to use to do things.
Consider squaring a number. If we wanted the square of 5
, we could do this:
answer = 5 * 5
or the square of 123498765
:
answer = 123498765 * 123498765
... that's a little wordy. It would be nice to only have to say the number once. So we could write it like this:
x = 123498765 answer = x * x
A variable is being introduced here. To get the answer
, we square x
. To get the square of any number we want, we just need to set x
to the right thing before this code!
Functions wrap this up for you. We could write squaring as a function like this:
function square(x) answer = x * x end square(5) print(answer) -- 25
You call a function in a way that looks like the way you defined it: square(5)
. Since you give it a 5
, when it runs, that x
will have the value of 5
.
Notice how the final line in the code you posted uses the name of the function, touch
:
script.Parent.Touched:connect(touch)
but notice that it isn't followed by ()
as thought it were a function call, like touch()
.
That allows ROBLOX to call it later with the value filled in.
For this event, it will call touch(
with the part that touched script.Parent
.
Since hit
is the name that argument was given, when the code runs, hit
is whatever touched, and you said hit:Destroy()
, so whatever touched gets :Destroy()
ed.