dude6354
|
  |
| Joined: 04 Apr 2009 |
| Total Posts: 205 |
|
|
| 29 Apr 2013 09:33 PM |
Can someone explain to me what "in pairs" means in lua? I was given this code on a post that i made in scripting helpers, and i understand everything but "in pairs". if someone could shed some light on this situation i would really appreciate it.
Also, here is that code i was talking about.
function HowManyAlive()
local Alive = { }; for _, Player in pairs(Game.Players:GetPlayers()) do if Player.Character:FindFirstChild("Humanoid").Health > 0 then table.insert(Alive, Player); end; end; return Alive; end;
if #HowManyAlive() == 1 then print(HowManyAlive()[1]); elseif #HowManyAlive() <= 0 then print("No one is alive"); end; |
|
|
| Report Abuse |
|
|
|
| 29 Apr 2013 09:37 PM |
for _, Player in pairs(Game.Players:GetPlayers()) do
_ = index Player = value
pairs(x) Where x is, you put a table
Basically, it loops through all the values in a table that you give it. Note that you can change _ and Player to w/e variable name you want it to be. |
|
|
| Report Abuse |
|
|
dude6354
|
  |
| Joined: 04 Apr 2009 |
| Total Posts: 205 |
|
|
| 29 Apr 2013 09:40 PM |
Oooooh! Thank you so much! That makes sanse lol. Also, mind if i ask one more quick question? to check and see if a name is in a table with an if statement, wouldnt it be:
if Player in Table then
or something like that |
|
|
| Report Abuse |
|
|
|
| 29 Apr 2013 09:51 PM |
The basic syntax is: for index,value in pairs(table)
So no, it wouldn't work.
You would have to be like...
for i,v in pairs(table) do if v.Name == Player.Name then return true end end
|
|
|
| Report Abuse |
|
|
dude6354
|
  |
| Joined: 04 Apr 2009 |
| Total Posts: 205 |
|
| |
|