PcoWSkbVqDnWTu_dm2ix

Nested Loops

Nested Loops

Nesting loops allows you to repeat tasks in batches. For example, baking three batches of six cupcakes, or assigning weapons to players on two teams. When loops are nested, scripts go line by line until it reaches the next loop. The inner loop will run until it’s condition is met before returning to the outer loop.

How Nested Loops Run

  • Outer loop: code runs line-by-line until it reaches the inner loop.
  • Inner loop: runs until its condition is met. If the loop is set to run ten times, it'll run all ten times before returning to the outer loop.


  • Outer loop: runs remaining code before starting the cycle over again.
  • Cupcake Baker

    If you wanted to code a game where players can bake multiple batches of cupcakes at a time, you can use one loop to control how many batches of cupcakes to make, and an inner loop to control how many cupcakes are made per batch. To see this in action, watch what happens with this script which makes squares instead of cupcakes.

    1. Create a new script named cupcakeBaker. Copy the code below.
    1. Watch as different color square is spawned for every cupcake.

    The print statement in the outer loop will run only one time per completed inner loop.

    Nested For Loop Tower

    Each loop has its own set of code, so can be responsible for different tasks. For example, instead of making the same cupcake, the outer loop changed the color of the cupcakes. One thing nested loops can do is change the placement of where an object spawns to create a tower like the one in this video. There’s three different loops, one each for controlling where along the width, length, and height of the tower the cube spawns.

    Code the Cube Maker

    For the cube tower script, first code a function that spawns a single cube. The tower will be built by repeatedly calling this function.

    1. Delete the cupcakeBaker script or disable it. If you don’t, there’ll be two scripts making parts at the same time in the same place.
    1. Create a new script named TowerBuilder. Add variables for tower size and cube size at the top.
    1. Add a local function named makecube() that creates a single square cube using CUBE_SIZE.
    1. Set the cube’s color to a variable which will be updated in the nested loops.
    1. Lastly, parent the new cube to the workspace so it appears in game.

    Working in 3 Dimensions

    The cupcakeBaker script had every cube spawn in the same location. As each new cube is made, previous cubes get pushed upwards, creating a mess. To create a tower, spawn cubes at specific points by setting the X, Y, Z properties of each new cube. X and Z are side to side. Y is up and down.

    Add X,Y, and Z Parameters

    1. Add parameters to makecube(), add parameters for spawnX, spawnY, and spawnZ. These numbers will set each new cube’s spawn location.
    1. Inside the function, set the cube’s CFrame property to a new CFrame using the spawnX, spawnY, spawnZ parameters.

    Spawn Cubes with Nested Loops

    The script will have three loops total, one each for the length, width, and height of the tower. To complete an entire floor before moving upwards, start with setting the Y coordinate in the first, outermost loop.

    1. Create a for loop to set how high each cube spawns.
      • Control variable: heightIndex = 1
      • End point: TOWER_SIZE
      • Inside the loop, add: local spawnY = (heightIndex - 1) * CUBE_SIZE
    1. With the first loop for height finished, start on the second. Inside the first for loop, add a new for loop for where to place the cube along the length of the tower.
      • Control variable: lengthIndex = 1
      • End point: TOWER_SIZE
      • Inside that loop add: local spawnX = lengthIndex * CUBE_SIZE
      • Make sure that every level of your loop is nicely indented like below.
    1. Inside the second loop, add a third for loop for the tower width. In this final loop, call makecube() and pass in the X,Y, Z parameters.
      • Control variable: widthIndex = 1
      • End point: TOWER_SIZE
      • Inside the loop add:
        • local spawnZ = widthIndex * CUBE_SIZE
        • makecube(spawnX, spawnY, spawnZ)
        • A wait time of 0.25 so you can watch the tower be built.
    1. To have each floor be a random color, change currentColor to random rgb numbers in the same loop in which you create a new floor.
    Tags:
    • coding
    • tutorials
    • loops
    • lua