Difference between different types of for loops

Hello! I am learning how to program in Lua 5 and I have been seeing a lot of different types of for loops in this forum. I know the basic for num = 1, 25, 1 do end for loops, but not the ones with for _, something in ipairs() do and for i, v in pairs() do. May you explain what the last two are, how they work, and what situations they use them. I’m trying to learn lua without youtube so I don’t just copy paste, and instead understand the code. Thank you, and I know this is not a well organized post but I’m glad you read this far.

That’s not a good decision not to use YouTube, because your question has been answered by many people on YouTube. Why are you asking on the forum instead of searching for Lua loops tutorial?

The difference between 1, 25, 1 do end and For i, v in pairs() do are this:

1, 25, 1:

Loops only a certain amount of times that you set it too

For i, v in pairs():

Loops as many times as here are things in a table such as game.workspace:GetChildren() and it loops through every single thing within the table or parent.

I see your point, but I want someone to clarify them in different words.

This page has a lot of info https://education.roblox.com/en-us/resources/pairs-and-ipairs-intro

1 Like

pairs and ipairs are what’s known as iterator functions. you pass them a table, and they facilitate iterating over it’s members internally.

local dict = {
    a = 1,
    b = 5,
    c = 4
}

for key, value in pairs(dict) do
    print('key: ' .. key .. ' value: ' .. value)
end

outputs:

  17:23:57.698  key: a value: 1
  17:23:57.698  key: c value: 4
  17:23:57.698  key: b value: 5

ipairs is similar to pairs, except it’s for iterating over numerically indexed values

local array = {
    'a',
    'b5',
    'c4'
}

for key, value in pairs(array) do
    print('key: ' .. key .. ' value: ' .. value)
end

outputs:

  17:25:54.075  key: 1 value: a
  17:25:54.076  key: 2 value: b5
  17:25:54.076  key: 3 value: c4

the underscore (_) variable name is a convention in lua that indicates an unused variable. For example; say we were only concerned with counting the previous array one by one.

local array = {
    'a',
    'b5',
    'c4'
}

for key, _ in pairs(array) do
    print(key)
end
1 Like

to add on to this; a deep dive into how pairs/ipairs work behind the scenes can be found in PIL

I used the website you sent me to try to create a list of the players to see if I can further understand it.

Output Error: 14:33:36.226 ServerScriptService.Script:2: invalid argument #1 to ‘pairs’ (table expected, got Instance)

a = game.Players

for i, v in ipairs(a) do

print(i, v)

end

pairs and ipairs must be passed a table. You passed a roblox instance. I think you meant to do local a = game.Players:GetPlayers() to make a an array of players

1 Like

oh. I tried :getService(“players”) and game.players and thought it was an array. Thank you!

Alright, I’m I did what you told me to do, and after trying to fix it with what I know, I couldn’t get it to work.

a = game:WaitForChild("Players"):GetPlayers()
for i, v in ipairs(a) do
	print(i, v)
end
wait(4) -- give the server time to load a player or two ingame

a = game:GetService("Players"):GetPlayers() -- use GetService() to access Service members

for i, v in ipairs(a) do
	print(i .. ' ' .. v.Name)
end

Your script should have worked so I’m assuming it just executed too fast. That’s basically the same thing as the example on the GetPlayers doc. image

Works! Thank you for explaining why mine might have not worked and solutions. I now understand it, and I’m 1000% more confident now using for loops and under that why “_” means.

2 Likes