Can some please explain parameters in a bit more detail?
Tutorials on YouTube just aren’t helping
Also, why would I need to use them with my functions?
Thanks,
–Luke
Can some please explain parameters in a bit more detail?
Tutorials on YouTube just aren’t helping
Also, why would I need to use them with my functions?
Thanks,
–Luke
Also before some one links me a devforum page…Please dont
I need more help
Parameters allow you to pass along information between functions, do you need a practical example?
Like variables listed under functions?
Say you want a function to do an animation but you want it to do any type of animation without being determined within the function like the animation ID and the player so you must pass the values through how much we call the function by placing it between the parentheses (Animation ID number, player)
in the function it is necessary to have at least 2 function parameters (ID, player) so you can use the ID to play the animation and the player to play the animation in the right player
Parameters are useful when you need to repeat the same action with different outcomes. If you wanted to print the type of different values, you could do something like this:
local function printType(arg) -- arg stands for argument by the way
print(type(arg))
end
printType(2) --will print "number" or "int" I believe.
printType("Hello") -- will print "string" because it's a string value.
Some things you might be confused on:
what are the differences between parameters and arguments? an argument is the data INSIDE the parameters. The parameters are basically the arguments as a whole, or you could just say everything between the parathenesis.
what does the type function do? It simply returns a string that is the type of the variable/ literal you put in the arguments. In simpler terms, type gives you a string that is named the type of value you passed in the function.
type("hi") -- returns the string called "string" since the value is a stringValue.
type(true) -- returns the string called "bool" or "boolean", I haven't really tested most of them.
type(20) -- returns the string called "number" or "integer", again, I haven't tested them.
you can also use Roblox’s Luau function “typeof” which is literally the same thing, but it supports everything that Roblox has made on their own.
typeof(Vector3.new(0,0,0)) -- will return "Vector3"
typeof(CFrame.new(0,0,0)) --will return "CFrame"
and so on.
Please ask anything more you’re confused on.
Does this help make it clear?
Soo function parameters are like the details for the function, so lets say you want to create a function that CREATES a part, however, you want something to be different every time you call a function, so this is a regular function:
local function createpart()
but we want to add a variable in the parameter, so this:
local function createpart(color)
if your adding more than once its this:
local function createpart(color, material, size)
so, when you call the function, you have to type in the variables FOR the function, so this:
createpart(Bright Blue, Bricks, 2)
so when the function is called, it makes a part which is Bright blue, with the material as bricks, and 2 studs sized on each brick
edit: Some functions set their own variables, for example, this:
Part.Touched:Connect(function(hit)
print(hit)
end)
So when THAT part is touched, it will print what is touched.
So its kinda like a variable inside a function that you lets just say… Do some math problem that determines what the variable’s “Value” is?
Its like a “reference” that keeps data moving within a “scope”
If you make a “local variable” inside a function, that variable cant be used outside of the function unless it is passed along somehow.
Parameters are a way to “pass by reference”
In my example code if I did:
function DamagePlayer(a, b)
a is gonna be a “reference” to that humanoid variable, and b is gonna be equal to 15.
Just keep in mind that the order in which you put the parameters does matter.
I cant be a equal to 15 if the first variable I am putting into the “call” is something else.
To “call” is to run something, such as run the function with the () triggering it.
Parameters are just basically passing information that’s been called by the function
Think of a Truck delivering stuff from Location A to Location B
function Delivered(Goods)
print(Goods) --Make sure that everything that has been delivered properly
end
local function Delivery()
local TruckGoods = {"Money", "Illegal Stuff", "Bananas"}
wait(10)
print("The goods have been delivered!")
Delivered(TruckGoods)
end
Delivery()
We’re passing the TruckGoods
variable inside the Delivered
function, and when calling it we set it as the first parameter
(Not sure if I got it right though with the local functions)
Local just changes the scope in which it can be used, so this would still work.