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 5 years ago
for i,v in pairs("")do
--Code
end

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
5 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 — 5y
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 — 5y
Ad

Answer this question