Looping Through Arrays

15 min

Looping Through Arrays

Picture of bridge parts disappearing and a diagram of loops

Arrays can be combined with loops, such as while or for loops, to repeat the same code for each value. For example, teleporting each player in an array, or making an array of parts catch fire.

The code below uses a for loop to teleport every player in playerArray, whether there’s five players or one hundred. By using #playerArray, the size of that array, you don’t have to know how many values are in the array.

To explore how to loop through arrays, you’ll create a disappearing path where every few seconds, a part from an array disappears.

Setting up the Project

Set Up the Disappearing Path

The project will include a series of parts that’ll disappear in the order they’re indexed.

  1. To store the parts and script, create a folder called DisappearingPath. In that folder, add another folder named Parts and a script named PathScript .
  1. In Parts, add at least three parts of a bridge renamed to the order in which they disappear, with Part1 being first and Part3 last.
  1. In PathScript, create variables to store DisappearingPath and Parts folders.

Create an Array of Parts

Instead of typing an entire array all on one line, each value in this array will be on it’s own line. Putting one value per line makes it easier to read when arrays become longer.

  1. To store the array of parts, type local pathArray = { and press Enter to autocomplete }.
  1. Between the { } of the array, type each part in the Parts folder, separated by commas. Remember the last value doesn’t need a comma. Also, because you’re storing these parts in an array, they don’t need to be in their own individual variables.

Looping Through Arrays

Use a for loop to make each individual part disappear in the order that they were added to the array. While you could type out each index to make parts disappear, using a loop reduces the amount of code needed and you won’t have to make changes if you add more parts later.

Create a For Loop

  1. To control how often a part disappears in seconds, create a variable named disappearRate. For testing, it’s best to keep it to a small number.
  1. Create a for loop with the following:
  • Starts: partIndex = 1, the index of the first value in the array.
  • Ends: #pathArray, the size of that array.
  1. So there’s a delay before a part disappears, in the for loop, add a wait() using disappearRate.
  1. To get a part to disappear, create a new variable named whichPart and set it equal to partsArray[partIndex] . Then, to disappear that part, set it’s CanCollide property to false and Transparency to 1.
  1. Check that parts of the path disappear over time.

Issue: Parts disappear too fast or are all gone at the start.

  • Make sure that disappearRate is greater than 1.
  • Depending on how fast your character loads into the game, the first parts may already be invisible. To address this, add a small wait, such as wait(2), at the start of the script.

Issue: Script crashes or has errors running.

  • Check that the name of each part in the array is exactly as seen in the Explorer, like partsFolder.Part1.
  • Check that each array value is separated with a comma, except for the last value.
  • Check that there are no spaces between special symbols, like # in #pathArray, or [ ] in pathArray[partIndex].

Create a Second For Loop

To make the path reappear, create a second for loop that will go through each part and instantly make each piece walkable.

  1. After the first loop, add a wait() to create a short delay before the path reappears.
  1. On your own, try coding a second for loop that makes the path useable again by changing each part’s CanCollide property to true and Transparency to 0. If you get stuck, check the code below:

  1. Check that once all parts disappear, they reappear.

Create a While Loop

To make the path disappear more than once, the for loops can be placed in a while true do loop.

  1. At the bottom of the script, create a new while true do loop. Move both for loops into the while loop.
  1. Check that once all parts disappear, they reappear.

Issue: The last path piece doesn’t disappear.

  • Check that there is a short wait between the two for loops. If not there, once the last piece disappears, the entire path will instantly reappear.

Issue: Script crashes or has errors running.

  • Check that both for loops are between while true do and the last end.

Optional Challenge

Challenge: Add more than six parts into the Parts folder and pathArray. Check that the code works as intended.

Finished Project Sample

Project File

Download the finished project here

Finished Script