What is the purpose of "for i = 1, 100 in pairs() do"?

I’ve been confused about this for a long time now. I don’t know the purpose of index in for loops, and what is the first value’s purpose (1) and the second value (100). What’s the purpose of it?

1 Like

https://education.roblox.com/en-us/resources/repeating-tasks-with-for-loops
that link should explain everything you need to know

1 Like

Simply, you’re defining the Start and End of the For loop.

local startIndex = 1
local endIndex = 100

for I = startIndex, endIndex do

end

This means the script will count from 1 all the way up to 100, or whatever other value you define.

One example use case for this would be for a gun.

You may have created a function that shoots a single bullet, but what if you want to fire a burst at once like a shotgun? Do a for loop.

3 Likes

for i=1 means that the variable i will start with being 1. With every loop it does, it will add 1 to the value of i, until i reaches 100. There is also a third optional value you can add after the 100 which is by how much i will increase with each loop.

Example:

for i=1, 100, 1 do
    print(i)
end

That will print numbers 1 through 100, increasing by 1 each time.

2 Likes

Like it will stop until it reaches 100?

2 Likes

But, When to use the for (variable) = value, value?

1 Like

well think about it, that last number is the increment so it should run 100 times

it starts at 1 and ends at 100, it adds 1 every time because of the increment
so it should be like this

1 2 3…98 99 100
1 Like

Oh. I’m getting it now. But I’m still not sure when to use for index = value, value though.

1 Like

You don’t do for i = 1, 100 in pairs() do, using pairs() is only for when you’re looping through a table of values:

local tab = { 1, 10, 100, 1000 }

for index, value in pairs(tab) do
    print("#"..index..": "..value)
end

It’ll print out:

#1: 1
#2: 10
#3: 100
#4: 1000

If you just want to count to a certain number you do this:

for index = 1, 10, 1 do
    print(index)
end

It’ll print out:

1
2
3
4
5
6
7
8
9
10

And if I change the last number to, for example, 5, it’ll count up by five:

for i = 1, 10, 5 do
    print(i) -- i usually means index.
end

And it’ll print out:

1
6

Change the starting number to 0 to make it count up by 5 the way you expected it:

for i = 0, 10, 5 do
    print(i)
end

And it’ll print out as expected:

0
5
10

To make it count to a higher number, just change the middle number to for exmple, 20:

for i = 0, 20, 5 do
    print(i)
end

And it’ll print out:

0
5
10
15
20

I hope this helped, I love using for loops, their incredibly useful and make so many things possible!

I tested all of the code here in a Lua interpreter.

5 Likes

But what is the third value (1) for though? (for index = 1, 10, 1 do)

1 Like

[EDITED]
You use “for i = num, num…” when you want to do a countdown, do something one by one without doing it all instantly, etc. I don’t use it much, but that’s all I can think of a use for that.

-- Countdown from 1 to 10
for i = 1, 10, 1 do
print(i)
task.wait(1) -- btw you can use wait(), but task.wait() is new and many people say it's better.
-- Output
1
2
3
4
..
end

[EDIT]: I’m too lazy to explain what’s pairs and ipairs. Perhaps, other people can explain it. Just know that pairs and ipairs are used for tables. Ipairs are for arrays and pairs are for dictionaries specifically.

https://education.roblox.com/en-us/resources/pairs-and-ipairs-intro#:~:text=pairs()%20and%20ipairs(),()%20is%20used%20with%20arrays.

1 Like

The third value is the ‘increment’ value. The increment value will either go up or down until it meets its’ end value.

for i = 1(Starting Value), 10(Ending Value), 2(Increment Value) do

If you want an example:

for i = 0, 10, 2 do
     print(i)
     task.wait(1)
end

Output:

0
2
4
6
8
10

Another example:

for i = 0, 5, 0.25 do
     print(i)
end

Output:

0
0.25
0.5
0.75
1
-- and so on.
1 Like

just a tip, you shouldn’t use decimals in the for loop because of floating point error

for i = 0, 20, 1 do
    i /= 4

    print(i)
end

this way even if there are floating point errors, the for loop won’t break

2 Likes

bannedplayers is an array so you should use ipairs, buckaroo

1 Like

pairs will still work with arrays, but you are right you should use ipairs

1 Like

I never said it would not work “should”, implicitly saying that it will work but really you ought o be using ipairs

1 Like

I think there’s a bit of confusion here. There are two ways to use a for loop, known as a generic for loop or a numeric for loop

The latter has easier syntax of
for counter = start, stop, step do

And generics use this syntax
for ... in function do

This is an example of a numeric loop

for i = 1, 10 do
  print(i) --> 1 2 3 4 5 6 7 8 9 10
end

for i = 10, 1, -1 do
  print(i) --> 10 9 8 7 6 5 4 3 2 1
end

And heres an example of a generic for loop, using the function ipairs

local list = {1,2,3,6,9,5}
for i, v in ipairs(list) do
  print(i, v) --> 1 1  2 2  3 3  4 6  5 9  6 5 (this is in pairs)
end

Generics can also take your own functions (they run until the inline function returns nil)

local function ipairsButLua(t)
  local c = 0
  return function()
    c+=1
    local getValue = t[c]
    if getValue then
      return c, getValue
    end
  end
end

local list = {1,2,3,6,9,5}
for i, v in ipairsButLua(list) do
  print(i, v) --> 1 1  2 2  3 3  4 6  5 9  6 5 (this is in pairs)
end

5 Likes

Everything you ever need to know regarding lua is right here:
https://www.lua.org/pil/4.3.4.html

2 Likes

https://www.lua.org/pil/4.3.4.html

2 Likes

I already stated this in my reply:

1 Like