Functions are sets of instructions that can be used many times in a script. There are premade functions like
wait() that are built into most programming languages. Coders can also create their own custom functions for code they want to use more than once. In Lua, this done by typing
local function NameOfTheFunction().
Functions can be thought of like a recipe. Like in real life, you only have to figure out the recipe once. Once you create the recipe, you can just follow the instructions and reuse it whenever you need. For example, if you were tired of telling your pet robot how to make your favorite dinner, you might make a function() that lists the steps. Then, whenever you want dinner you would just say, "Robot, run
|Steps to Making Dinner||Function|
|1. Boil water
2. Add pasta
3. Cook 15 minutes
4. Add sauce
5. Sprinkle gummy bears on top
6. Serve on a plate
To practice, you’ll create a function that prints your favorite food to the Output Window.
Setting up the Script
Create a new Script
- Create a new script in ServerScriptService. This script can be used in any project.
- Rename the script FunctionsPractice.
- Delete “Hello World”.
- Write a descriptive comment at the top. For example:
-- Prints your favorite food
Start a Function
All functions should have names that help you remember what their purpose is. You’ll create a new function named printFood() that will print your favorite food to the Output Window.
local function printFood().
Naming Rules - Functions
Functions use camelCase with the first letter lowercase and next words capitalized.
- Examples: addPoints(), restartGame(), rainFlamingLlama()
Press Enter to autocomplete the function and add
Check That Functions Have an End
If the function doesn’t have an end, your code won’t run. Type end yourself if the code doesn’t autocomplete.
Adding Code to Functions
All of the code for your function has to be typed between
local function printFood() and
End. Any code not between those two points won’t run when the function does.
Type a Print Command
local function printFood() and
end, create a print statement that states your favorite food.
Indenting in Functions
As you typed, your code was indented. This makes it easier to see the start and end of a function.
If you run the code right now, nothing will happen. There’s one more thing we need to do before the function will work.
Call the Function
Functions won’t run until they are called by name in the script. They’re pretty patient like that. To call a function, type the function’s name including the
() at the end.
Under end, type
Test the code. Your favorite food should appear in the Output window.
- Check that your favorite food is between printFood() and end.
- Make that your food has quotations on each side.
- Check that printFood() is typed after end.
Your code should now be:
Checking if Code is in Scope
Any code that is outside a function is out of scope and won’t run when that function is called. To be inside scope, a function’s code needs to be between
local function functionName() and
To see what this looks like, do this experiment:
Create a print statement outside of the scope of the function.
Under your second print statement, call the function you created three times.
Test your code. The print statement within the function will print three times. The print statement outside the function will only run once.
Check That Your Code is in Scope
Whenever you create functions, always make sure that your instructions are in scope for that function. If code is typed outside
local function functionName() and end, it won’t run when it’s called.
These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.
Previous Reusing Code with script.Parent Next Using If/Then Statements