Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What exactly do parameters do in functions?

Okay, so I have a basic knowledge about functions, and ROBLOX Lua in general. So while I was looking around in the forums, I noticed a lot of things like this

game.Players.PlayerAdded:connect(function(player)

I've read the ROBLOX Wiki to see if I could understand from what it says there, but I still have absolutely no idea. Some help would be much appreciated.

1 answer

✓ Accepted answer

What is a Parameter?

A Parameter is any value taken in by a function. These are the same values that you will use as Arguments for calling the function.


How do I Define a Parameter?

The definition of a function's Parameters occurs in the definition of said function. The syntax for defining functions, as you may know, is this;

function [function_name] ()

[code]

end

See the parentheses former to the declaration of the function's name? Any and all parameters that you want the function to contain will be defined between the bounds of the parentheses.

function Greet(String)
	--[[
		The function 'Greet' can now take in an argument. 
		The argument will be equivalent to 'String'.
	]]
	print(String)
end

How Do I Use a Parameter?

The way I see it, there are two perspectives you must adapt while working with functions. The definition of the function, and the calling of the function.

To actually use a parameter you need to expand your perspective to the Calling side. The function that was defined above took in the parameter 'String', and printed it. So now all that's left is to call the function, and provide the arguments.

Greet("Hello") --> Hello

Look in your output window, you should see the word "Hello" pop up.


Correlation to Your Example

Now that you have some insight on what parameters are, as well as how to define and use them, I can elaborate on your specific example;

game.Players.PlayerAdded:connect(function(player)

What you have provided here, is the definition of a PlayerAdded event. The parameter 'player' is going to be equivalent to whatever player had joined when the event was fired.

You can use the key phrase 'player' to access the newly added player.

2 comments

2

Oh, so from what you said, it seems that the parameter is a value that is assigned later in the script? This is one of the best explanations I think I've ever seen on this site. Thank you, kind sir.

0

No problem mayann.

This discussion is preserved as it was. New answers, comments, and votes are closed.