Iterating through Objects

Use 'for' loops to iterate through objects

by Dr_K4rma

Author Avatar


To understand the contents of this topic, you need to know several things.

First off, lets assume that we have such a setup:


We will declare it as:

local TutorialGroup = workspace.TutorialGroup

TUTORIAL - Understanding 'for' Loops

Lets assume we want to change the color of each brick to a 'Bright red' brick color. Conventionally, most scripters would find themselves doing this:

local Part1 = TutorialGroup.Part1
local Part2 = TutorialGroup.Part2
local Part3 = TutorialGroup.Part3

Part1.BrickColor ="Bright red")
Part2.BrickColor ="Bright red")
Part3.BrickColor ="Bright red")

This may seem as a viable option at first. However, what if instead of three objects, you have ten, or even a hundred?

The method above suddenly becomes unusable.

However, there is a way to get around this, which is what is being showcased in this tutorial. Simply put, a for loop allows to commit an action a certain amount of times.

If I were to compare its functionality to a while loop, it might look something like this:

local i = 1

while i < 3 do
       i = i + 1

   print(i.." Hello")

is the same as

for i = 1, 3 do
   print(i.." Hello")

Both return the same response.

1 Hello

2 Hello

3 Hello

But the for loop takes a bit less code handles some things better than a while loop.

For instance, using 'pairs', we can iterate through the elements of a table.

local Table = {"Hello", "gek", "third", "etc"}

for i, Element in pairs(Table) do

In turn, the code would print out

1 Hello

2 gek

3 third

4 etc

The 'i' becomes the 'index' or the number of the element in the table and the 'Element' becomes the value of the Element

(In this case, a string)

Coming back to the initial example, 'TutorialGroup' is a group of three parts. However, by doing

local IterationGroup = TutorialGroup:GetChildren()

we can iterate through the 'children' of the TutorialGroup since ':GetChildren()' returns a table of children

that an object has.

If we were to rewrite the original code we had for changing the color of the bricks to red, it would now look something like this:

local TutorialGroup = workspace.TutorialGroup
local IterationGroup = TutorialGroup:GetChildren()

for i, Part in pairs(IterationGroup) do
   Part.BrickColor ="Bright red")

Much simpler, right?

Often times, the laziest way of doing something can be the best way. Let the script do the work for you.

This method opens up a whole plethora of oppertunities for lazy scripting.

Let me give demonstrate another example.

Lets assume that instead of turning the brick red, we want it to print "Touched" and the brick name anytime it is touched.

We would be able to do the following:

local TutorialGroup = workspace.TutorialGroup
local IterationGroup = TutorialGroup:GetChildren()

for e, Brick in pairs(IterationGroup) do
      print("Touched "..Brick.Name)


Thats it in a shellnut!

Hope I helped you in your scripting endeavors -- and remember, lazy scripting is the best kind of scripting!

As always, if you're still having trouble, the ROBLOX API and the ROBLOX Developer Forums are an invaluable resource.

View in-game to comment, award, and more!