PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Prerequisites Students should have completed Create a Script
Lesson Time 5 - 10 minutes
Optional Handouts Coding with Lua
Learning Objectives
  • Create a function that prints a customized line of text
  • Understand how to use scope to troubleshoot writing functions
  • Functions are sets of instructions that can be used many times in a script. There are premade functions like print() and 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 makeSpaghetti()".

    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
    makeSpaghetti()

    To practice, you’ll create a function that prints your favorite food to the Output Window.

    Setting up the Script

    Create a new Script

    1. Create a new script in ServerScriptService. This script can be used in any project.
    2. Rename the script FunctionsPractice.
    3. Delete “Hello World”.
    4. 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.

    1. Type local function printFood().

      functions1_01.png

    Naming Rules - Functions

    Functions use camelCase with the first letter lowercase and next words capitalized.

    • Examples: addPoints(), restartGame(), rainFlamingLlama()

    1. Press Enter to autocomplete the function and add end.

      functions1_02

    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

    Between local function printFood() and end, create a print statement that states your favorite food.

    functions1_03.png

    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.

    1. Under end, type printFood().

      functions1_04.png

    2. Test the code. Your favorite food should appear in the Output window.

      outputFunctionExample.png

    Troubleshooting Tips
    • 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:

    -- Prints your favorite food
    local function printFood()
    	print("Curry!")
    end
    
    printFood()

    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 end.

    To see what this looks like, do this experiment:

    1. Create a print statement outside of the scope of the function.

      scopeExample01.png

    2. Under your second print statement, call the function you created three times.

      scopeExample02.png

    3. Test your code. The print statement within the function will print three times. The print statement outside the function will only run once.

      scopeExample03.png

    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.