Looping Through Arrays
Looping Through Arrays
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.
- To store the parts and script, create a folder called DisappearingPath. In that folder, add another folder named Parts and a script named PathScript .

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

- 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.
- To store the array of parts, type
local pathArray = {
and press Enter to autocomplete}
.
- 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
- 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.
- 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.
- So there’s a delay before a part disappears, in the for loop, add a
wait()
usingdisappearRate
.
- To get a part to disappear, create a new variable named
whichPart
and set it equal topartsArray[partIndex]
. Then, to disappear that part, set it’sCanCollide
property tofalse
andTransparency
to1
.
- Check that parts of the path disappear over time.
Troubleshooting Tips »
Issue: Parts disappear too fast or are all gone at the start.
- Make sure that
disappearRate
is greater than1
. - 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
[ ]
inpathArray[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.
- After the first loop, add a
wait()
to create a short delay before the path reappears.
- On your own, try coding a second for loop that makes the path useable again by changing each part’s
CanCollide
property totrue
andTransparency
to0
. If you get stuck, check the code below:
Second For Loop Solution »
- 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.
- At the bottom of the script, create a new
while true do
loop. Move both for loops into the while loop.
- Check that once all parts disappear, they reappear.
Troubleshooting Tips »
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 lastend
.
Optional Challenge
Challenge: Add more than six parts into the Parts folder and pathArray
. Check that the code works as intended.
Final DisappearingScript »
Previous Creating and Using Arrays Next Making Changes to Arrays