| |
|
|
| 19 Feb 2017 04:22 PM |
Its a loop mostly used for tables.
|
|
|
| Report Abuse |
|
|
|
| 19 Feb 2017 04:24 PM |
So for say you stored something in the table then you just wanted to acces something v basically means you go through each value,string thats in the table.
|
|
|
| Report Abuse |
|
|
|
| 19 Feb 2017 04:26 PM |
Btw i meant to say if you wanted it to go through all the values,strings etc.
|
|
|
| Report Abuse |
|
|
|
| 19 Feb 2017 04:26 PM |
i and v are a commonly-used convention for the index and value variables of the table you're iterating over
pairs() is an iterator function that returns both the index and value of any value in a table that isn't nil
|
|
|
| Report Abuse |
|
|
|
| 19 Feb 2017 04:27 PM |
^ Unsub explained it better then i could lol as unsub is pretty advanced.
|
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 19 Feb 2017 04:35 PM |
Here's the pastebin link because this is probably going to be censored: pastebin/YF3DXMEE
i and v are variables commonly used when indexing. i is the Key, v is the Value for that table.
"for i,v in pairs" is just the same as "for apple,orange in pairs()" where apple is th###ey and orange is th###alue
They can be anything, even non alphanumerical characters like underscores, _ you may have seen for _,###n pairs(table) do
That's just because th###ey isn't important so th###reator just decided to blank it out with an underscore (t####e###s##h###o########n##h###able) We just want the ###i########e####ual value of that point on the ###le)
so if we had a table, like
local myTable = {"apple","orange","pear"}
--and we said
for _,v in pairs(myTable) do print(v) end
-- it would print >apple >orange >pear
it would print the same thing if we said
for foo,bar in pairs(myTable) do print(bar) end
Now you might ask, when is the key important? If we know we want something at a specific numerical key on the table (its "locus" if you will) then we can say for the same table
print(myTable[2]) -- with the key in brackets > orange
or print(myTable[3]) > pear
We can loop through the contents of a table without needing "in pairs()" We can instead do something like this
for i = 1,#myTable do print(myTable[i]) -- where i is another arbitrary variable, it might as well be anything end > apple > orange > pear
This also works with children of an object. To get all the children of an object, you use the :GetChildren() function to return an array of every child within that object.
Example, say we have a Model with 5 parts as children named Part1 through Part5.
local my####### model:GetChildren() -- if we could look at table, it would look like this myTable = {Part1,Part2,Part3,Part4,Part5}
Now we can index this either way for i,v in pairs(myTable) do print(v.Name.." is located at ke##########n the table") end > Part1 is located at key 1 in the table > Part2 is located at key 2 in the table > etc...
or we can say
for i = 1,#myTable -- where #myTable is the length of the table print(myTable[i].Name.." is located at key ####### in the table") end
> Part1 is located at key 1 in the table > Part2 is located at key 2 in the table > etc...
|
|
|
| Report Abuse |
|
|
|
| 19 Feb 2017 04:38 PM |
These guys couldnt of explained it any better
|
|
|
| Report Abuse |
|
|