I was reading This article, which was very informative, but left me with one question. What do the words “for” and “in” do in loops? for instance, with the typical for loop we always say “for i, v in pairs”, so what do they mean in this loop? That seems to be a crucial part of making custom factories, but I don’t really get it.
if I have
for i,v in ipairs() do
end
ipairs returns a function that will run each loop, and “i,v” are the values said function returns.
The function you put after “in” does not have to be a built in function. It can be your own function but it must return an “iterator function”, which is just a function that will be called each iteration of the loop
local function myfunc()
local function iterator() -- this function gets called each iteration
return a,b,c,d,e
end
return iterator
end
for a,b,c,d,e in myfunc() do
end
That’s what I gathered from skimming through that post. I’ll read it more in depth
99% of the time you can use the built in functions to iterate over tables.
Well there, for loops, oh they are so fun.
Let’s take this table for example
local MyFavoriteFoods = {"Pizza","Steak","Hugs"}
Well I want to find all the entries in this table exactly. I could use #MyFavoriteFoods
but that would find the amount of things that are my favorite food. So, let’s use a for loop!
So what I can do is plop this in
for i,v in pairs()
end
Well, what’s so special about that? This is a loop that iterates through a table If I plug my table in, it’ll go through the table, and print the results.
Here’s my example
local MyFavoriteFoods = {"Pizza","Steak","Hugs"}
for i,v in pairs(MyFavoriteFoods) do
print(i,v)
end
--OUTPUT
[1] = "Pizza"
[2] = "Steak"
[3] = "Hugs"
How is this useful?
Well this is useful in several ways. Say I want to get everything in a folder and delete it. I can use the :GetChildren()
function, and do this!
local MyFolder = workspace.Folder
for i,v in pairs(MyFolder:GetChildren()) do
v:Destroy()
end
Well how does this work? For loops iterate through tables. GetChildren returns a table of all the children.
Frequently Asked Question: How does i,v work?
Well these are the variables you set.
They can be any variables you want!
a,b
c,e
or even
_,g
Do what you like, but that’s the basic of for loops, but me trying my best to explain it.
Let me break it down.
for basically means what it says.
in basically also means what it says.
To make it make sense you can think of it like this:
Instead of for i, v in pairs, think For EveryNumberWhichCorrelatesItsPart, AndTheirPartName InsideOf. That’s the best I can describe it.
for = For
i, = EveryNumberWhichCorrelatesItsPart,
v = AndTheirPartName
in pairs = InsideOf
If you don’t understand you can run a script using the code (I am not using studios idk if this works). This is just to help you learn if you want.
while true do
wait(1)
for i, v in pairs (game.workspace:GetDescendants) do
print(i…", "…v)
end)
end
Very good question, I see none of the responses truly answer your question so I will attempt to do my best in helping.
A for loop iterates through a table and to a number in Lua, other languages however give the ability to iterate strings and even vectors.
There are 3 functions we can use in lua to iterate a table:
next
ipairs
pairs
Each of these has its own uses, I use next just because it’s more primitive, and does what pairs do. Pairs iterate a table without any order, the indexes can be strings or numbers, however, Ipairs iterate a table in order from x-x+1. This is useful for keeping tables organized especially when dealing with player data because as we know, HashTables and Arrays have different uses.
The for loop also allows us to iterate numbers from x to y. This is useful for a countdown or other things.
Now, let’s talk about the structure of a for a loop.
for Index, Value in next, {} do
print(Index, Value)
end
for Number = 0, 100, 10 do
print(Number)
end
Now, for is the standard convention for a loop introduction. When iterating over tables in is the introduction to the method call after, the function attached to the method call will return the values we assign. For instance, for I,V in pairs() pairs is our method call in is the caller for that method and I,V is what is returned from pairs. The same applies to next, and ipairs.
Now, when dealing with numbers, for Number = 0, 100, 10 do is the normal convention. The = takes places of in for this format, and that returns counting from 0-100 in 10’s. The first number is the starting place, the second number is the ending place, the third number is the amount to count by.
Now, do is the conclusion to the loop, it is used after we establish what the for loop is going to do.
So, when referencing custom functions with that logic we can write a simple function to return 3 arguments instead of 2.
local Table = {
[1] = {1,2,3};
}
function Loop(Table)
local Index = 1
local EndIndex = #Table
local function Iterate()
if Index ~= EndIndex then
Index += 1
return Table[Index][1], Table[Index][2], Table[Index][3]
else
return Table[Index][1], Table[Index][2], Table[Index][3]
end
end
return Iterate()
end
for X,Y,Z in Loop, Table do
print(X,Y,Z)
end