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

Explain a parameter please?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago
script.Parent.Touched:connect(function(hit) --Is this a parameter??

Also, explain to me what they are and what they do, please and thanks :)

2 answers

Log in to vote
0
Answered by
funyun 958 Moderation Voter
8 years ago

Think about math functions. You give them inputs, and they give you outputs.

function f(x)
    return x + 4
end

print(f(3)) --7
print(f(6)) --10
print(f(-2)) --2

function g(x)
    return x^2
end

print(g(4)) --16
print(g(5)) --25
print(f(g(4))) --g(4) = 16; 16 + 4 = 20

It's basically the same thing in RBX Lua.

script.Parent.Touched:connect(function(ThePartThatTouchedScriptParent)
    print(ThePartThatTouchedScriptParent.Name)
end)

Or

function onTouched(part)
    print(part.Name)
end

script.Parent.Touched:connect(onTouch)

If you're not going to do anything with the part that touched, don't even bother giving the function parameters.

function onTouched()
    print("hey")
end

script.Parent.Touched:connect(onTouch)
Ad
Log in to vote
7
Answered by 8 years ago

An argument is the value you're returning to a function with parameters. Parameters are the variables you assign to the function as you're declaring it.

function test(x,y,z) -- "x","y","z" are the parameters
    print(x,y,z) -- display them in the output window
end

test(1,2,3) -- "1","2","3" are the arguments we're returning to the function "test", which will read this as: x = 1, y = 2, z = 3 (specifically because we returned the arguments in the same order that corresponds to how we set up our parameters in the actual function)

You see arguments every time you execute a function by giving it a value of some sort. You can actually execute a function (while returning an argument) in 3 ways:

{}

Yes, you can call a function with a table. It will also return that table to the function, like this:

function test(some_table)
    print(type(some_table)) -- prints the data type of what we returned
end

test{} -- this will call the function, along with passing that table as an argument

""

This you've probably seen before. Calling a function with a string, while also returning that string (you can see this by creating a new script, and the first line of code should say something like 'print 'hello world!')

Example:

function test(some_string)
    print(some_string)
end

test'hello world!' -- this will print 'hello world'

() (Obviously)

And as i showed you with my first example, you can use the parenthesis to return multiple values of varying data types.

Events

Now obviously in your case, it's gonna be a bit different since events have functions that have specific parameters that are returned to the callback, when that event is fired. But it's not something we wouldn't expect when this is happening ...

For example, the "Touched" event has 1 parameter that represents what part touched it. That argument is returned to the function we connect it to. Like this:

-- Assuming this script is inside a part

script.Parent.Touched:connect(function(Hit)
    print(Hit) -- Hit is just the variable, and can be anything. The important thing is to know, it's the first argument (and in this case, the only one)
end)

All in all, arguments and parameters can most of the time be used interchangeably.

Hope this helped.

Answer this question