Hi, I've always been wondering something.
What is the
for i,v do ----Help?
for?
I always see it in scripts and I would love to learn more about it.
for i,v do Examples
table={"hello","there","how","are","you"} for a=1,#table do print(table[a]) end --OR for i,v in ipairs(table) do print("This is in table number "..i.." and is "..v) end --OR for i,v in next, table do print("This is in table number "..i.." and is "..v) end
This is known as a generic for loop -- it iterates, or basically scans, over each item in a table. Simple enough, but how do you use this?
Let's take a look at this code:
Words = {'3XTR3M3 ', 'M3M3 ', 'Z0N3'} for i, v in pairs(Words) do print(Words[i]) -- 3XTR3M3 M3M3 Z0N3 end
How does that accomplish printing all of the items?
Well, unlike a regular for loop, I and V aren't strictly numerical values. They also represent what the for loop is currently iterating over. i is the number (in order of the table, for example M3M3 is 2) of the item it is iterating over, so naturally it printed all the strings in sequence.
v, on the other hand, represents what the item it's iterating over is. This can be extremely useful. Heres a favorite of all scripters, a for loop that iterates through each part in a model and making them anchored.
for i, v in pairs(Model:GetChildren()) do if v:IsA('BasePart') then v.Anchored = true end end
for
is a kind of loop in Lua. A loop is a block of code that will happen over and over.
for
is designed to do something for each value in something.
As explained elsewhere, there are two types of for
loops.
for variable = start, stop do
or for variable = start, stop, step do
. This will give you each number in the range start, start + 1, start + 2, ..., stop
or start, start + step, start + step * 2, start + step * 3, ..., stop
. This is a separate structure from the other kind of for
loop.
Numeric for loops do not use in
.
Generic for loops do use in
.
The generic for loop uses something called an iterator. Iterators repeatedly give you values. The main iterators in Lua are pairs
and ipairs
. They traverse tables, meaning they give you each thing in the table.
Remember that the best way to think about for
is that it does something for each something.
pairs(tab)
produces two things each iteration: a value
in the table tab
, and the index
where the value
is in tab
.
This means you can find everything inside of a table object using a for
loop with pairs.
As the manual explains, generic for loops look like this:
for variables in iterator do -- do stuff end
Commonly, the variables will be written i, v
-- short for index, value. Note: despite the frequent habit of calling these i
and v
, those are bad names -- they are not descriptive.
Here is an example showing how to get everything out of a table:
local tab = {5, "banana", apple = "green"} for index, value in pairs(tab) do print("tab[", index, "] = ", tab[index], "=", value) end -- tab[ 1 ] = 5 = 5 -- tab[ 2 ] = banana = banana -- tab[ apple ] = green = green
Each iteration gave one more piece of the table.
A practical example would be do something to each player in the game. For instance, assigning players to random teams.
Frequently, you don't care about the index
. You can write _
to indicate to a human reading your code that you don't need the value.
for _, player in pairs(game.Players:GetPlayers()) do if math.random(2) == 1 then player.TeamColor = BrickColor.Red() else player.TeamColor = BrickColor.Blue() end end