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: What "i v in pairs do" stands for?

Previous Thread :: Next Thread 
Salvestroni2 is online. Salvestroni2
Joined: 25 Jul 2013
Total Posts: 12483
19 Feb 2017 04:21 PM
titile


hi
Report Abuse
QuantumNexsus is not online. QuantumNexsus
Joined: 09 Nov 2014
Total Posts: 2451
19 Feb 2017 04:22 PM
Its a loop mostly used for tables.


Report Abuse
QuantumNexsus is not online. QuantumNexsus
Joined: 09 Nov 2014
Total Posts: 2451
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
QuantumNexsus is not online. QuantumNexsus
Joined: 09 Nov 2014
Total Posts: 2451
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
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
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
QuantumNexsus is not online. QuantumNexsus
Joined: 09 Nov 2014
Total Posts: 2451
19 Feb 2017 04:27 PM
^ Unsub explained it better then i could lol as unsub is pretty advanced.


Report Abuse
Soybeen is not online. 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
QuantumNexsus is not online. QuantumNexsus
Joined: 09 Nov 2014
Total Posts: 2451
19 Feb 2017 04:38 PM
These guys couldnt of explained it any better


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