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.