Learning the For Loop

The past year was the year i learnt the most about roblox programming, and i’m very happy with the knoledge i have now.

But there’s still a lot of things i don’t know about scripting, and there’s one of them that i already should know but i don’t, wich is the for loop.

So if someone can show me a good tutorial, articles or places i can learn how the for loop works, i would greatly appreciate.


Which of the for loops? o for i, v or for i?

I would say both. Because i don’t know any of those

^^ Most of the work should be explained here with much greater detail.

In Lua there are two types of for loops. The first type takes a starting value, an ending value, and and an incrementor value.

for number = 5, 10, 1 do

--[[Expected Output:



This loop would start at 5, and assign that value to number. Then it runs through the code between do and end. Once that is done it then increases the value of number by 1 because that final number is the incrementor value. And because it’s a positive one it will add 1 to 5. This process repeats until number is greater than or equal to the ending value. In this case it’s 10.

The other type of for loop takes an array and will loop through every value/index pair in that loop. And similar to the for loop above, it will assign those value/index pairs to two variables that can be used in the for loop’s code.

local ShoppingList = {"Milk","Pear","Bread","Cheese"} --Anything that returns an array will work for this for loop, just making a simple one here.

for Index,Value in ipairs(ShoppingList) do --We assign the for loop to look through this array and run the code below for each entry in the array.
     print(Index.." - "..Value)

--[[Expected Output:

1 - Milk
2 - Pear
3 - Bread
4 - Cheese


Let’s start with i, v loops. This is used to loop through multiple things in a group and doing something for all of them. For example, if we had a model with many parts in it, we could loop through all of them and change their transparency.

local model = --model 

for i, part in pairs(model:GetChildren()) do   
      -- automatically makes variables for i and "part"
     -- i is how many parts you have sequenced through so it could be 1 or 5 etc.
     -- part variable is the current part that it is looping through.
       part.Transparency = 0.5

Basically we tell it to loop through all of the children of the model using model:GetChildren(). Then it makes a variable named part for the part that it is currently looping through.

1 Like

The for loop actually what it does is it will loop through for each item inside of whatever is after pair(i.e if u use GetDescendant() on a particular object), think of it as an array, and you loop through each position you can grab whats inside, It’s essentially the same thing except with actual objects, when doing the for loop it will set the current item you’re looking at to v not i.
here is an example. I have a model which has lot of particle emitter but i want to enable them all at the same time this is where for i v comes

local model = game.Workspace.Model ---define our model
                          ---we used getdescendant() do get all children including their own children
for i, v in pairs(model:GetDescendant()) do
      if v:IsA("ParticleEmitter") then ----check if its an emitter
           v.Enabled = true --enabled it and then it will restart again till everything is enabled

Hope u understand :slightly_smiling_face:

1 Like

The for i loop lets you run a command for certain times.

for i = 1, 10, 2 do

The first 1 is the beginning, the 10 is the end and how many times the loop will run, and the 2 is the increment, the increment decides how it will count, example: if I put 2 it will count 2 in 2 until you reach 10

for i, v in pairs(workspace:GetChildren()) do

“i” stands for workspace, and “v” stands for childrens in workspace
So, i is the index, and v is the value


If you know how to do the for i = 1, #list, 1 do loop, you basically know all the for loops. In my opinion, the for loop is the most useful loop and is usually the most used.

1 Like