FOR_PAIRS


This loop will repeat the code after do for each entry of the table provided and it will also set both the key and the value for each table entry as the local variable names you give them after for.


for key, value in pairs( table ) do
key a local variable name (used within this loop) that will hold the key name (left column data)
value a local variable name (used within this loop) that will hold the value data (right column data of the table)
table variable name of a numbered table

Example Table:

HighScores
john 143
bob 99
kim 984

This is how you loop through this table, being able to access and use both the keys (names) and values (scores):

for name, score in pairs( highscores )
	print( name )
	print( score )
end

-- prints: john 10 bob 143 kim 984

Images in this Guide by NerdyTeachers is licensed under CC BY-SA 4.0

1270

18 Mar 2023

Font