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: for blah, blah in pairs

Previous Thread :: Next Thread 
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 08:09 PM
So... I see some people do this

for i,v in pairs...


What type of loop is it? what does it do?
If someone says "index" explain that as well.
I mean.... is i,v a variable? Or is it required?
Report Abuse
iLiminate is not online. iLiminate
Joined: 26 Feb 2012
Total Posts: 388
21 Nov 2013 08:12 PM
Say you have a list:
blah = {"a","b","c"}
doing
for i,v in pairs(blah)
print("Index:"..i,"Word:"..v)
end
Index would be like.
blah[1] = "a", because the first([1]) item in blah is "a". Hope this helped!
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:12 PM
i, v are 2 variables

think about it like this

function x()
return 123, 456
end

i, v = x()
print(i, v)
>123 456
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 08:13 PM
cool i didnt know return was blue word, what does it do?
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
21 Nov 2013 08:14 PM
It is a for loop.

pairs is a iterator function.

i and v are variables and can be anything.

so the basic format is:

for Variables in Iterator do

end

---
Pairs returns two variables. The first one the index. The second one the value.

So:
for Banana,Apple in pairs(TABLE) do

end

let's say TABLE = {"Pop","Bed","Cookie"}
In this table Pop is the first element. Bed is the second and Cookie is the third.

Using pairs the first variable refers to the location/index of the item in the loop.

The second variable is the current value in the table being looped through.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:16 PM
it returns a value or multiple values or tables or anything really

function lol()
return "CNT"
end

print(lol())
>>CNT
Report Abuse
iLiminate is not online. iLiminate
Joined: 26 Feb 2012
Total Posts: 388
21 Nov 2013 08:17 PM
EDIT: Completely botch this up. =P
Fix:
Say you have a list:
blah = {"a","b","c"}
doing
for i,v in pairs(blah)
print("Index:"..i,"Word:"..v)
end
This would print out:

Index:1,Word:a
Index:2,Word:b
Index:3,Word:c

Index would be like.
blah[1] = "a", because the first([1]) item in blah is "a". Hope this helped!
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
21 Nov 2013 08:17 PM
Return could return uhh...Pretty much any variable for value.

Tuple,Table,nil,Int,Float,Instance,Any other Roblox object.

Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 08:20 PM
hmm
In what case would I use it in?
Can u give me an example telling me EXACTLY what it does, above was quite thorough, but did not explain what the script did.
Lemme try:

local Chips = {
"Spicy",
"Mild",
"Nacho" }

For lol,LOL in pairs (Chips) do

now what
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:21 PM
print(LOL)

>Spicy
>Mild
etc
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 08:21 PM
what would

print (lol) do?
the same?
Report Abuse
iLiminate is not online. iLiminate
Joined: 26 Feb 2012
Total Posts: 388
21 Nov 2013 08:22 PM
print(lol) would print
>1
>2
>3
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:22 PM
No, it would print the index
Like
>1
>2
>3
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
21 Nov 2013 08:23 PM
It loops through every item in the table.

So

for Octo,Item in pairs({"Bob","Joe"}) do
print(Item,"Is at the "..Octo.." Index")
end

Or you could loop through all the players.

This would kick ALL players.

And btw GetPlayers() return a table with all the players.

for _,Player in pairs(Game.Players:GetPlayers()) do
Player:Kick()
end
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 08:27 PM
?!?!?!?!? Do both of them mean the same thing?

would

_:Kick() Work?

anyways, I found this

while wait() do
for k,v in pairs(game.Players:GetChildren()) do
if v.Character then
Char = v.Character:GetChildren() -- ITS V! WAHT DOES IT DO?! would k work?
for i = 1,#Char do
if Char[i].ClassName == "Hat" then
Char[i]:destroy()
end
end
end
end
end



It seems to remove hats,
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 08:28 PM
No, it would print the index
Like
>1
>2
>3

didnt see that, lol
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:28 PM
in that script, k would
return the position of the player in the table
so
print(k)
>1
>2
and till the amount of players
Report Abuse
iLiminate is not online. iLiminate
Joined: 26 Feb 2012
Total Posts: 388
21 Nov 2013 08:28 PM
v represents the player name.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:29 PM
Lim
Tt represents that actual player, not the name
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 08:30 PM
what could I use index for?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:30 PM
It depends, not much really, unless you have an associative table

local foods = {
banana = 5,
orange = 10
}

for food, price in pairs(foods) do
print(food, "costs: $", price)
end
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
21 Nov 2013 08:31 PM
I rarely use it...Hard to think of an example. It is usually just there and ignored.

But it is useful sometimes.
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
21 Nov 2013 08:32 PM
And no _:Kick would error. _ is a number variable.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Nov 2013 08:32 PM
It's actually used more than people think
When peope do

local players = game.Players:GetPlayers()
local randomPlayer = players[math.random(#players)]
Then they are using the index to get a player.

Let's say I joined first, then you did:
1 - cntkillme: game.Players.cntkillme OR game.Players:GetChildren()[1]
2 - you: game.Players.you OR game.Players:GetChildren()[2]
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
21 Nov 2013 09:02 PM
but you didnt use in pairs.... D:

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