What are parameters and why should I use them?

Hello, I am a beginner scripter and I’ve never really known why I should make a specific function to do one thing and why and when I should use parameters. For example:

local function Addition(a,b)
    print(a + b)
end

Addition(1,3)

And I dont know why and when to make a function for a specific thing. For example:

local function PartThatTouchedTheObject(OtherPart)
    print(OtherPart)
end

script.Parent.Touched:Connect(PartThatTouchedTheObject)

Could anyone help me understand why and when I should use these?
Thank you!

In Lua, a function is used to make what is called a sub-program.
There are two types of subprogram:
Procedure

  • A procedure is used to simplify code - if you have something you want to do multiple times. This is generally used if you have lots of code to group together that will be used more than twice (I’d say).

It’s a bit misleading in Lua, both types are called function when written in code.

--This example is moving players to a part in the Workspace.
local function tpPlayers()
   for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
        player.Character.HumanoidRootPart.CFrame = workspace.Part.CFrame
   end
end

Then there’s the other type: a function. This is similar to a procedure, but returns a value.

--Addition example

local function add(numberOne, numberTwo)
    return numberOne + numberTwo --return the added result to where it was requested
end

local answer = add(5, 7)
print(answer) --outputs "12"

They are more helpful in more complicated things, especially when you start using ModuleScripts. Some events pass parameters when connected to a function or procedure, for example the Players service’s PlayerAdded and PlayerRemoving, which both pass the player as the parameter. These are crucial for data saving and loading.

local players = game:GetService("Players")

local function load(player) --Parameters passed by PlayerAdded (the player joining, in this instance)
    print("@"..player.Name.." has joined the game!")
end

players.PlayerAdded:Connect(load) --Invoke the function when the event fires

This example displays that the player has joined the game - the player was acquired through the parameter.

Other examples are in other events, like a part’s Touched event.

local function onTouch(hit) --hit is the part that touched
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
        print("@"..player.Name.." touched the part.")
    end
end

In the two examples, parameters are crucial for the code to run. As I said earlier, they are also incredibly useful when simplifying code, where procedures and functions need to do the same thing multiple times. They slot well with variables.

Parameters can be passed to both types of subprogram. They tend to be more useful in functions (I find).

Here’s the documentation:
Functions and Events | Documentation - Roblox Creator Hub

I’ve written a lot here, someone please point out if I missed anything…

i believe it is

local function PartThatTouchedTheObject(OtherPart)
    print(OtherPart)
end

script.Parent.Touched:Connect(PartThatTouchedTheObject, otherPart)

using function automatically connects a function to the event, so the name of your function would just be the name you chose for the other part in the touched event
i might have typed it wrong because i never did something like this

You did… the Connect function only takes 1 parameter which is the name of the thread to connect to.

You’d need to connect to another subprogram to put both in.

alright, but how do you would you do something like that though, because im pretty sure its possible, or is it as you provided, with no additional argument?

Sorry, I put the function there by accident since i’m used to doing

script.Parent.Touched:Connect(function()
    
end

yea, now i also understood how to do it, so it would just be

local function functionName(otherPart)
         -- rest of the code
end

script.Parent.Touched:Connect(functionName)

however you can still do it the way youre used to, i dont think it makes much of a difference, but for more complex functions, not ones connected to a touch event, for example something like a function in a modulescript, parameters are nearly always required, so i think you got all info you need from 12345koip in his reply

(I’m pretty sure this is correct)

You would probably have a middle subprogram to pass it along.

local function targetThing(hitPart, number)
    --do stuff
end

local function middleSubprogram(hit)
    task.spawn(targetThing, hit, 5)
end

part.Touched:Connect(middleSubprogram)

It’d just be more efficient to have the other parameter, in this case 5, as a variable accessible by the whole script.

i didnt think it that much, thats a very smart way, if it works of course

1 Like