What does for i, v in pairs() and ipairs do

Ohhhhhhh, I get what v does now

1 Like

the i thing is the index which is the number of the instance (1,2,3,4,5,etc)

so it prints the number of the value?

for example: you have 50 instances inside workspace, it will keep printing from 1 until its 50.

it just prints out its index, aka, its number in line

oh I think I get what the i, and v does now
but how about the ipairs()?

ipairs() works in the i,v format (index, value),

pairs() works in the k,v format (key, value).

ipairs() ensures that the indices are iterated in numerical order (1, 2, 3, 4, 5,etc)
pairs() returns all keys and values out of order (5,2,4,1,3,etc)

Iā€™ll be away from my laptop for minutes

1 Like

thats a better explanation than mine ngl.

1 Like

pairs() is not ā€œkinda unpredictableā€, pairs() both traverses Array and Dictionary parts of a Table, meanwhile ipairs() only traverses the Array part of a Table.

Due to the new syntax update which allows Luau to do the iteration without pairs() internally, the usage of pairs() is no longer necessary. However, if you still want to iterate through the Array part of a table, you need to use ipairs().

Thank you for correcting me. I genuinely did not know that.

1 Like

Iā€™m back and thanks for the explanation ANDRO

1 Like

I understand how both work now thank you all for the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.