I’ve been learning scripting for a bit now but i cannot seem to wrap my head around how parameters and Arguments work and how to use them.
Whenever I find a video on them Im still as clueless as before i watched it.
Does anyone have a good way of explaining them?
Ex: local KillBrick = script.Parent
KillBrick.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
hit.Parent.Humanoid.Health = 0
end
end)
What is hit doing in this script?
Edit: Thanks all of you for your explainations these are so helpful!
Parameters → The data you receive
Arguments → The data you send
They are basically used to that functions have a context they can work with. Without parameters / arguments, the function is very limited in its usage.
local function squareNumber(num)
-- takes in a number and outputs its square
return num * num
end
print(squareNumber(10)) --> 100
print(squareNumber(5)) --> 25
... and so on
-- 10 and 5 are arguments
-- `num` is a parameter
The example you specified is an interesting case. I’d say it’s more special, because we’re now dealing with events i.e., event callbacks.
Events (such as KillBrick.Touched, formally BasePart.Touched) are triggered by the Roblox Engine itself. The Connect method (member function) takes in a callback, which is simply a function that will be executed when the event gets triggered. Depending on the event, data will be supplied to our callback.
In this case, since we are using BasePart.Touched, the first parameter will be the other part which has touched KillBrick.
Since the purpose of this script is to kill players, we must first check if the touching part (otherPart / hit) belongs to a character model. We do so by looking in this part’s Parent for a Humanoid. The existence of a Humanoid instance will signify that the part we touched belongs to a valid character, and not something completely arbitrary.
After that, it’s all a matter of setting the Humanoid’s health to 0, killing the character.
function printParameter(Parameter)
print(Parameter)
end
if you just called this function with nothing else like: printParameter().
It would just print nil because parameter wouldn’t = to anything
But if you called it like: printParameter(5). It would print 5, do you see how it works?
You get the option to include parameters, or not. In this case I chose to add the parameter myParam this means whenever I call the function, in between the parenthesis I can add a value that will be stored as myParam within the function.
So if I have this function:
local function add(a,b)
return a+b
end
local sum = add(3, 5)
print(sum) -- 8
When I call the function with add(3, 5) I pass in 3 and 5 as arguments which then get stored in the parameters a and b in the function. So basically in the function a=3 and b=5 because those are the values we assigned.
An easy way to think of parameters are as variables that are declared when defining your function and then assigned when you call it.
Arguments
Now a lot of people use the ters Parameters and Arguments interchangeable but they are technically not the same. As @PhoenixRessusection said:
So basically, back to my example from before:
local function add(a,b)
return a+b
end
local sum = add(3, 5)
print(sum) -- 8
Here in the function declaration (local function add(a,b)), a and b are parameters.
Later, when I call the function (local sum = add(3,5)), 3 and 5 are arguments that get passed to the function to be stored as a and b.
I hope you get it.
As For hit
Ok so some built in roblox functions and events will have built in arguments that get passed through that you can store in parameters. In this example, roblox automatically passes a in an argument which you are choosing to store in a parameter called hit (note this name does not have to be hit, it is just a common name for this specific parameter). This hit is actually a reference to the object in which just touched the part!
Another Cool thing:
One more cool thing you can do is have an unspecified amount of parameters using the ...:
Example:
local function add(...)
local nums = {...} -- unpack our arguments
local sum = 0
for _, num in nums do
sum += num
end
return sum
end
print(add(3,5,8)) -- 16
If you dont understand the ... dont worry about it right now!
The function keyword uses two brackets next to it, which are the Parameters.
local function foo(arg1, arg2)
print(arg1 + arg2)
end
Simply speaking, parameters behave like variables, but ONLY inside of that function.
local function foo(arg1, arg2)
print(arg1 + arg2)
end
print(arg1) -- arg1 is outside of the function, therefore this variable has no meaning.
When you execute it, you can provide input to pass on to the function.
local function foo(arg1, arg2)
print(arg1 + arg2)
end
foo(5, 7) -- arg1 = 5 and arg2 = 7. This function prints out 12.
Those are called arguments.
When using an event such as .Touched, it’s like a script checking when the part is touched, and then running the function. And because they can run the function, they can also provide arguments, which is why you receive a hit parameter when you use .Touched