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

Help with arguments?

Asked by 8 years ago

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.

1
"Hit" or the argument of Touched, is the part that touched said part. So whatever part hit the script's parent would be "hit". If you touch the part with your legs, then your legs become "hit" because they are the blocks that touched the part. Discern 1007 — 8y
0
@Overdash | It is the part that touched the part, not "they are the blocks." Redbullusa 1580 — 8y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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.

Idea behind arguments

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.


Connections

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.

Ad

Answer this question