for k, v in pairs

Well… what does it mean? I’ve seen it in several scripts but the Gmod Lua Wiki is showing up nothing except examples.
Speaking of examples, here is one:

[lua]for k, v in pairs(player.GetAll()) do
Msg( v:Nick() … "

Tells you everyone who is on the server players name.

Nevermind Cubar and his obvious answer, he’s the resident troll.

I see, thanks very much :slight_smile:

One of two loops in lua. It cycles through everything in a table (in your example case, player.GetAll()). Variable 1, often called v, is the current value of the table, whereas k is the table key for v. Extremely useful, much more than a standard for loop.

A for in loop doesn’t necessarily loop through every pair in a table, it depends on what iterator you use.


I consider while and repeat as loops as well. That makes 4. :v:

I prefer using the numeric for when I know exactly how many elements are in a table, and that table is indexed with sequential integers.

[lua]local SuperTable = {6,53,622,1}

function a(t)
for i = 1,4 do

function b(t)
for k, v in ipairs(t) do

function c(t)
for k, v in pairs(t) do

a is more efficient then b in this scenario. c is the least efficient.


Yeah but they’re used a lot less. Actually I’m not even sure what their whole syntax is. :frown:

– code


[lua]while b do
– code

– code
until b[/lua]

Whoa there, Quebec.

What’s the difference between

for k, v in ipairs


for k, v in pairs

Does the “i” have any significance?

ipairs is a different iterator. pairs loops through each key and value pair in a table in an order that isn’t guaranteed. ipairs loops through each key and value pair by increasing the key by one each time, therefore it needs to be a numerically indexed table. Sorry if that confuses you, I’m sure Crazy can explain better.

Yes it does. ipairs will only go over tables indexed with sequential integers while pairs will work with any kind of key for the entries in the table.

Also there is no guarantee in what order pairs will return values.


I think I’m with you so far.

Now for the idiot question…
can you define “key”?
New to Lua and don’t understand some of these terms.

If you don’t know what it is, print it.
[lua]local t = {“bacon”,“apple”,“tomatoes”,“pizza”}
for key, value in pairs(t) do

Output :
1 “bacon”
2 “apple”
3 “tomatoes”
4 “pizza”

local t = {}
t[“a”] = 53
t[“bingo”] = 5323
t[“waterbottle”] = 1

for key, value in pairs(t) do

Output :
“a” 53
“bingo” 5323
“waterbottle” 1[/lua]

The key is the name you use to refer to a value inside of a table.

Just to add to what Crazy said, a key (or an index) can be of any Lua type. for example, these are all valid:

[lua]local t = {} – t is an empty table
local k = {} – k is an empty table
t[k] = “my key is a table”[/lua]

[lua]local t = {}
local k = 1
t[k] = “my key is a number”[/lua]

[lua]local t = {}
local k = false
t[k] = “my key is a boolean”[/lua]

[lua]local t = {}
local k = “key”
t[k] = “my key is a string”[/lua]

They can even be functions as functions are first class Lua values.

[lua]local t = {}
local k = function() return 1 end
t[k] = “my key is a function”[/lua]

Questions I have looking at this thread:

How are functions differentiated? Is it similar to as if you parsed the source of the function as a string, trimmed it, and compared it?

Is there any point to doing

– things

I wasn’t troll. :hurr:

Wrapping things in a do/end block will make a new scope for whatever is inside them.

local fe = 1
fe will be nil because the stuff inside the do/end block had a different scope than the code that contained it.

They are differentiated by their memory address. A function is only equal to itself.