Wrap a job in a named function, give it inputs, and call it whenever you need that job done.
Here's a script that gives a player coins twice:
local coins = 0
coins = coins + 5
coins = coins + 5
It works, but imagine doing this for fifty features. Copy-pasting the same lines everywhere is how bugs are born. Functions are the cure: you write the job once, give it a name, and call it whenever you need it.
function addCoins(amount)
coins = coins + amount
end
Reading it left to right:
function — I'm about to define a job.addCoins — its name. You call the job by this name.(amount) — the parameter: a box the function gets filled when you call it.coins = coins + amount — the actual job.end — the job stops here.Defining a function does nothing by itself. You have to call it:
addCoins(5) -- gives 5 coins
addCoins(10) -- gives 10 coins
One function, two calls, two different results. The magic is the parameter — every call fills the box with a different value.
What does defining a function by itself do?
These two words get used interchangeably, but here's the precise version: parameters are the names in the definition (amount), arguments are the values you pass at the call site (5, 10).
You can have as many parameters as you need:
function giveReward(playerName, amount, reason)
print(playerName .. " earned " .. amount .. " coins for " .. reason)
end
giveReward("Sam", 100, "winning")
In function giveReward(playerName, amount, reason), the names inside the parentheses are called .
Nearly every useful function is: take inputs, do work. Even the built-in tools are functions:
print("hi", 5) -- takes whatever, shows it
tonumber("10") -- takes a string, returns a number
You've been calling functions since the second lesson. print(...) is a function with a variable number of inputs.
Define a function named cheer that prints 'Go!' whenever it's called, then call it once.
end. Every function needs its end. Luau errors — the Output tells you the line, and usually it's the missing end.addCoins() when the function needs (amount) gives you nil inside, and nil + 5 errors.addCoins and never calling it means nothing happens.Next: getting answers back from a function with return.