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 » Scripting Helpers
Home Search
 

Re: What is 'for a, b, in pairs' used for?

Previous Thread :: Next Thread 
AtlanticBlox is not online. AtlanticBlox
Joined: 12 Jun 2013
Total Posts: 5136
06 Aug 2013 04:04 AM
I forgot what to put after 'in pairs' also.
Report Abuse
SethDusek5 is not online. SethDusek5
Joined: 26 Mar 2011
Total Posts: 2580
06 Aug 2013 04:50 AM
for index,value in pairs(table_name) do
print(index, value)
end
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
06 Aug 2013 04:51 AM
for a, b in next, table do

Thats better.
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
06 Aug 2013 05:00 AM
You guys all can't explain how it works? You're just going to give him examples? What is this?
Report Abuse
AtlanticBlox is not online. AtlanticBlox
Joined: 12 Jun 2013
Total Posts: 5136
06 Aug 2013 05:01 AM
I understand that 'for' is a loop but what does it mean with 'in pairs'.
C'mon, I'm onlya beginner at scripting.
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
06 Aug 2013 05:34 AM
In Lua, there is a type of for loop called a "generic" for loop. It's generic because it can be used to do anything (as opposed to the "numeric" for loop which can only count with numbers). Generic for loops work with special functions called _iterators_. Iterators are used to iterate (step) through a table.

In the following example, we have an iterator called `countThrough`. This iterator "counts" through a table `t`. It does this by incrementing an integer `n` until there is no value at `n` in `t`. Generic for loops have a special way of working with iterators; they call the iterator recursively with a constant, which is the object that is being iterated through, as well as the value(s) that the iterator may return.

    function countThrough(t, n)
        print("iterator: ", t, n);
        n = n + 1;
        if t[n] then
            return n;
        end
    end;

    local myTable = {3, 6, 2, 8, 4};
    for index in countThrough, myTable, 0 do
        print("loop block:", index, myTable[index]);
    end

Look in the output to see what this iterator prints, and to see how it works.

I also wrote up this special factory function which makes better syntactic sugar for getting children:

    function children(object)
        local childList, n = object:GetChildren(), 0;
        return function(childList)
            n = n + 1;
            if n <= #childList then
                return childList[n];
            end
        end
    end

    for child in children(workspace) do
        print(child.Name);
    end

Look in this section of lua.org for more cool stuff: http://www.lua.org/pil/7.html
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
06 Aug 2013 05:37 AM
Hehe I just fixed it up:

    function children(object)
        local childList, n = object:GetChildren(), 0;
        local l = #childList;
        return function()
            n = n + 1;
            if n <= l then
                return childList[n];
            end
        end
    end

    for child in children(workspace) do
        print(child.Name);
    end
Report Abuse
AtlanticBlox is not online. AtlanticBlox
Joined: 12 Jun 2013
Total Posts: 5136
06 Aug 2013 05:42 AM
My head went blank after reading (and alalysing a little bit with my scripting knowledge) that.
Report Abuse
SethDusek5 is not online. SethDusek5
Joined: 26 Mar 2011
Total Posts: 2580
06 Aug 2013 05:55 AM
Look, here is how in pairs can come in handy in a ban script.

banned = {"Noob", "Haxxor"}

game.Players.PlayerAdded:connect(function(plr) --runs when a player enters
for i,v in pairs(banned) do
if plr.Name == v then -- If the table's value matches the player's name then
plr:destroy() -- Removes the player
end
end
end)
Report Abuse
Aerideyn is not online. Aerideyn
Joined: 16 Jan 2010
Total Posts: 1882
06 Aug 2013 08:28 AM
Blocco is correct of course - though maybe these sort of formal explanations can be a bit overwhelming if you are new to scripting :p
I know "in pairs" took me a while.. but it really is a simple concept once you get it
(Assuming you know how to loop through a table already, if you don't then in pairs is going to have to wait..) You will wonder how you ever got by without it!


Anyway ..
Think about how you would loop through a table the manual way..

Numbers = {1,3,5,63,23,46,2,34,6}

for index = 1 , #Numbers do
value = Numbers[index]
print(index , value)
end

output>

1 ,1
2 , 3
3 , 5
4 , 63
5 , 23
6 , 46
7 , 2
8 , 34
9 , 6

now at it's most simple form (the rest can wait..) in pairs does exactly this - here is the equivalent code.

for index , value in pairs(Numbers) do
print(index , value)
end

run it and watch as you get the exact same results!
So why do we have in pairs at all? are we so lazy to need a new way to loop through index and value in a pair just so we can save little typing?

It turns out in pairs is a lot more useful than this.
The above example used "numerical" indices, that is we indexed each value with a number.

Tables in lua can have any index..

Sword = {
["Damage"] = 10,
["Price"] = 1000,
["Owner"] = "Telemon"
}

(if you haven't got this far with tables yet then that is fine.. read up on tables on the roblox wiki :) Learn them.. love them.. )

Just think.. how on earth would we ever loop through this table?
Sword[1] is nil! there is no value for sword at [1], or [2].. or any number!

in pairs to the rescue!
In pairs will with its deep magic (iterators) work, no matter what type of index you use.
Just remember - that because we are not using numbers any more in pairs may spit it out in whatever order it likes.

for index,value in pairs(Sword) do
print(index,value)
end

output>
Owner , Telemon
Damage , 10
Price , 1000

And this is why it is the "generic" for loop.. it works with generic indicies.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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