Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use "for i,v in pairs("") in a script?

Asked by 6 years ago
1for i,v in pairs("")do
2--Code
3end

This code has been in my head for a while and I really want to know how to use it. Can someone give me a link or answer my question?
Thanks.

1 answer

Log in to vote
0
Answered by
sydre 229 Moderation Voter
6 years ago

for i,v in pairs() is used whenever you want to loop through a table or dictionary. For example:

local MaTable = {“Thing 1”, “Thing 2”, “Thing 3”}

for i,v in pairs(MaTable) do – this will go through “Thing 1”, “Thing 2” and “Thing 3”

the i and the v in the loop is variables for the loop. i stands for how many time the code have gone through the table

for i,v in pairs() do
print(i)
end – this will print:
1
2
3
Because it looped through the table three times.

the v in the loop stands for what item the code is on

for i,v in pairs(MaTable) do
print(v)
end – this will print:
Thing 1
Thing 2
Thing 3
Because those are the items in the table

0
Thanks! HomieFirePGN 137 — 6y
0
`Because it looped through the table three times.` Not exactly, those're the indexes for the values. Otherwise, using dictionaries we'd recieve a number instead if that were the case. TheeDeathCaster 2368 — 6y
Ad

Answer this question