Alerite
|
  |
| Joined: 14 Jan 2013 |
| Total Posts: 1848 |
|
| |
|
|
| 02 Jul 2016 01:26 PM |
I sort of explained it here: http://forum.roblox.com/Forum/ShowPost.aspx?PostID=191814884 and also here docs.google DOT com/document/d/1KlrlNYNfVKyvTbmJBoVODOqMJF-wTn_JbGUEm0G6RIM |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 02 Jul 2016 01:29 PM |
Iterates through a table of values. Tables are useful because they hold a bunch of information for example:
local t = {"hello", 1, "bye", "asdf", 435} --this is a table of values
for i, v in pairs(t) do --the two variables can be whatever you want. I and V are just most common because they stand for index and value
--This code will run one time for each value inside the table print(i) --i is the index of the current iteration print(v) --v is the value of the table at place i print(t[i]) --this is the same as printing v because the value in table t at position i is v end
Output
>1 --i >hello --v >hello --t[i] >2 --i >1 --v >1 --t[i]
et cetera and it would go through all values.
Certain built in things return tables for example GetChildren(), this can be useful for indexing all the children of a certain instance. For example if you want to print the names of everything in workspace do this:
for i, v in pairs(workspace:GetChildren()) do print(v.Name) end
Pairs is useful for a lot of things. Check out the wiki page if you need more help or ask questions here.
|
|
|
| Report Abuse |
|
|
|
| 02 Jul 2016 01:31 PM |
tl;dr: pairs can be used to get all the key-value pairs in a table:
local tbl = {"a", "b", "c", a = 1, b = 2, c = 3} for key, value in pairs(tbl) do print(key, value) end -- 1 a -- 2 b -- 3 c -- a 1* -- b 2* -- c 3*
Asterisks imply it may not be necessarily in that order. |
|
|
| Report Abuse |
|
|
|
| 02 Jul 2016 01:53 PM |
pairs is a function, it returns the table and 'next'. next is a function in Lua that returns the next value in a table. So
for i,v in pairs(t) do
is the same as
for i,v in next, t do
What happens after this is it repeatedly calls next until it returns nil (e.g. finds what Lua thinks is the end of the table)
That's overly simplified, if you want a more detailed explanation send me a chat when the site chat comes back up :) |
|
|
| Report Abuse |
|
|
|
| 02 Jul 2016 01:53 PM |
| Oh it's back up. Send me a chat whenever xD |
|
|
| Report Abuse |
|
|