generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Can someone explain "for i,v in pairs" to me?

Previous Thread :: Next Thread 
Alerite is online. Alerite
Joined: 14 Jan 2013
Total Posts: 1848
02 Jul 2016 01:21 PM
What does it do, exactly?


Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
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 is not online. 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
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
02 Jul 2016 01:53 PM
Oh it's back up. Send me a chat whenever xD
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image