Could someone please give me a semi-detailed explanation on what the words mean inside the function brackets?
function( this word here )
Could someone please give me a semi-detailed explanation on what the words mean inside the function brackets?
function( this word here )
It is a parameter and there can be multiple of them seperated by a "," (Comma). It acts like a variable and can hold a value. When calling the function, you can send in an argument for the parameter.
local function SayThis(WhatToSay) -- WhatToSay would have a value of Hello because it was passed through the function
print(WhatToSay) -- Because WhatToSay has a value of Hello, it would print Hello
end
SayThis("Hello") -- Hello is the argument you want pass to the parameter
The parameter can also stay unused so when calling the function, you dont always have to pass in arguments. In this case, the parameter won't have a value and would be nil.
local function SayThis(WhatToSay) -- No argument was passed so it would have no value (nil)
print(WhatToSay) -- Because WhatToSay has no value, it would print nil
end
SayThis() -- No argument passed to the function
You can use multiple parameters.
local function SayThis(First, Second, Third) -- The parameters now hold the value of the arguments in their order.
print(First) -- "Hello"
print(Second) -- "Bye"
print(Third) -- We only sent in 2 arguments so the third parameter has no value
end
SayThis("Hello", "Bye") -- You can send in multiple arguments
Sometimes, there are default arguments already sent to the function when it is fired like during an event.
script.Parent.Touched:Connect(function(partparameter) -- Each event have different types of arguments for the parameters. When the script.Parent is touched, the function is called and the part that touched the script.Parent is sent through the function as an argument.
print(partParameter.Name) -- Would print the name of the object that touched script.Parent. If a tree touched script.Parent, then this would print "tree"
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
okay that makes sense, guess it's just a case of learning what parameters go with what function as shown in Example 4, thanks a lot man : )