What do parameters do in a function?

Hi guys! I’m a beginner scripter and I made my first teleport block with a tutorial. This is the code I used:

local PartOne = script.Parent.part1
local PartTwo = script.Parent.part2

PartOne.Touched:Connect(function(a)
if a.Parent:FindFirstChild(“HumanoidRootPart”) then
a.Parent.HumanoidRootPart.CFrame = PartTwo.CFrame + Vector3.new(0,3,0)
end
end)

I understand everything except for the parameter of the function (which is ‘a’ here). I know the parameter can literally be any letter, and it can be used as variables that are substituted when you print and calculate a number, but I’m not quite sure how the parameter of the function works in this script. Please explain to me how this works… thank you :sob::pray:

1 Like

Hello happy to hear you starting your journey with scripting.

local PartOne = script.Parent.part1
local PartTwo = script.Parent.part2

PartOne.Touched:Connect(function(a)
if a.Parent:FindFirstChild(“HumanoidRootPart”) then
a.Parent.HumanoidRootPart.CFrame = PartTwo.CFrame + Vector3.new(0,3,0)
end
end)

In this Touch function paramater “a” is a part that touched “PartOne” for example parameter ‘a’ is left leg.
Here is usefull document about functions
Functions | Documentation - Roblox Creator Hub
Also when you post script I advise using ``` because its looks better
I hope it helps!

3 Likes

This script is actually pretty complicated so I will translate it into another one which does the same thing for illustration.

local PartOne = script.Parent.part1
local PartTwo = script.Parent.part2

PartOne.Touched:Connect(function(a)
		if a.Parent:FindFirstChild(“HumanoidRootPart”) then
			a.Parent.HumanoidRootPart.CFrame = PartTwo.CFrame + Vector3.new(0,3,0)
		end
	end)

To:

local PartOne = script.Parent.part1
local PartTwo = script.Parent.part2

function MyFunc(a)
	if a.Parent:FindFirstChild(“HumanoidRootPart”) then
		a.Parent.HumanoidRootPart.CFrame = PartTwo.CFrame + Vector3.new(0,3,0)
	end
end

PartOne.Touched:Connect(MyFunc)

When you create a function with out a name, like function(), the function is said to be “anonymous”, it does not get a name. It can only be used exactly where it appears. By assigning it a name “MyFunc” we can use it the same way later. We can also do the same thing with the name on the left, like an ordinary variable:

local MyFunc = function(a)
	...
end

The list of names between the parentheses are formal parameters. They can have any unique name as you’ve noted. When a function is a called, you provide an existing name as an actual parameter (argument):

--This is sometimes named a call site, a location where a specific function is used.
MyFunc(myPart)

The formal parameters are then bound to the actuals from left to right. In some languages, the number of parameters (arity) of both the function and the call must match. In Lua, this is not the case and any formal paremeter which does not have an actual will have the value nil.

In your full code, Connect is a function which takes another function (MyFunc) as its first actual (argument). This makes it a “higher-order” function. Connect’s job is to delay the calling of MyFunc until a specific event happens, in this case the Touched event of PartOne. It will then call MyFunc using actual parameters that are specific to that event. Since you can’t see this call site, what those actuals are, are documented here:

It passes the other part which is involved in the Touched event.