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: Point of In Pairs

Previous Thread :: Next Thread 
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:04 PM
What is the exact point of using In Pairs in a script and what can you use it for?
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
05 Mar 2015 06:04 PM
Is is an iterator function. You use it in generic for loops.
Report Abuse
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:08 PM
So I looked at it on the wiki... So it stops when there are no more values to print in the item :/ Is it possible you could give an example the one in the wiki makes sense yet I still cant get the idea.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
05 Mar 2015 06:08 PM
for i,s in pairs (game.StarterPack:GetChildren()) do
if s:IsA("Tool") then
s:Remove()
end
end

--iterates every single thing inside the given variable (Starterpack) and puts them in "pairs" or rather for every item, add the variable s.

--finds every tool in the starterpack and removes it.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
05 Mar 2015 06:10 PM
instead of using i (index), use something like _,s in pairs instead so you can use the variable i for something else in the code.
Report Abuse
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:11 PM
So all its children = the variable s. Right?
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
05 Mar 2015 06:14 PM
Correct.
Report Abuse
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:16 PM
Is there a way you could use this and give each one a different variable name so you could find one individually or give it a numerical value and print the number of children
Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
05 Mar 2015 06:23 PM
for i,v in pairs(table)do
print(v) -- v as in value
print(i) -- i as in index, the place on the table the value is it
end
Report Abuse
FunkiAndrew is not online. FunkiAndrew
Joined: 01 Dec 2013
Total Posts: 2318
05 Mar 2015 06:24 PM
woah... we got the same hat... anywho,

local ting = 0
for _,v in pairs(game.workspace:GetChildren())do
ting = ting + 1
print (v,ting)
end

output:

1
2
3
4
5
6
7
8
9

and so on
Report Abuse
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:24 PM
Ok thanks I've been wondering the point of pairs for awhile now I think all my questions are answered for now. :O
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
05 Mar 2015 06:25 PM
I know the way I do my for loops, its probably inefficient but it still gets the job done nonetheless. The loop counts everything for the given variable as v. If you want to define something else with a different variable then you will have to use another loop. Variable v could be anything, A part, a model, whatever it is in the game.

lets say there are 5 bicks in the game.

Bricks are called 1,2


for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name == "1" then
print("we found brick 1")
elseif v.Name == "2" then
print("we found brick 2"
else --if both statements are false
print("we found none of the bricks in the game")
end
end

Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
05 Mar 2015 06:25 PM
@Andrew

Not the best way to explain it.


Anyway what it does is go thiough evenything in the table, and the value in the table is the second variable and the place in it is the first, these can be named anything you want.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
05 Mar 2015 06:27 PM
FunkiAndrew, I agree with MrJoey. You shouldn't teach it that way. You get the index variable for a reason, and then you go and create a system that would be much simpler (and better (and even better when teaching)) to use the index variable.
Report Abuse
FunkiAndrew is not online. FunkiAndrew
Joined: 01 Dec 2013
Total Posts: 2318
05 Mar 2015 06:30 PM
yeah...

for loops were tricky for me, they got me confused.

for _,v in pairs(workspace:GetChildren())do -- btw, 'workspace' can be used instead of game.Workspace
print (v.Name)
end

prints the same thing as

for _,v in next, workspace:GetChildren() do
print (v.Name)
end

some people prefer next, others prefer pairs. it's really your choice.
of course, there IS ipairs, which I don't have much experience with...
Report Abuse
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:31 PM
so for example it'd be like this?
t = (1,2,3,4,5)
for _, v in pairs do (t)
print (t)
-----------------------------
the output would be
1
2
3
4
5
right?
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
05 Mar 2015 06:32 PM
parts = game.Workspace:GetChildren()

for i=1,#parts do
print(i)
end

Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
05 Mar 2015 06:33 PM
for i,v in pairs(TABLE_NAME) do
print(v.Name..' Is at the position of '..i..' In the table')
end
Report Abuse
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:35 PM
I understand for loops using numerical values to know how many times it will do it.
like
for i = 1, 2 do
print "easy"
it will print it two times
I just never understood pairs but I think I understand it now.
Report Abuse
superconscript is not online. superconscript
Joined: 15 Mar 2012
Total Posts: 413
05 Mar 2015 06:37 PM
so a pairs loop would go like
t = (1,2,3,5,6)
for _, v in pairs (t) do
print (t)
--------------
Output
1
2
3
5
6
Right?
Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
05 Mar 2015 06:38 PM
_,v

i,v

potato,corn

-- values
print(v)

print(v)

print(corn)

-- numbers
print(potato)

print(i)

Report Abuse
FunkiAndrew is not online. FunkiAndrew
Joined: 01 Dec 2013
Total Posts: 2318
05 Mar 2015 06:38 PM
RIGHT! oops caps
Report Abuse
Dr01d3k4 is not online. Dr01d3k4
Joined: 11 Oct 2007
Total Posts: 17916
05 Mar 2015 06:40 PM
A table is just a list of key-value pairs. You give a table a key (like tbl[key]) and it gives you a value, or you set the value for a key (like tbl[key] = value).

A dictionary is something like
local person = {
name = "dr01d3k4";
id = 72297;
favouriteColour = "blue";
};


An array looks like:
local array = {"hello", "world", "this", "is", "an", "array"};
but this is just shorthand for
local array = {[1] = "hello", [2] = "world", [3] = "this", [4] = "is", [5] = "an", [6] = "array"};


The simple explanation of what pairs does is allow you to iterate through these key-value pairs:
for key, value in pairs(person) do
print(key, value);
end
This would output (tested in online Lua 5.3 demo)
id 72297
name dr01d3k4
favouriteColour blue

Note that dictionaries don't guarantee the order will be the same as how you wrote it.
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