Unable to loop through a table using ipairs

So, today I was working on fixing a bug with my terrain generator, when I was extremely frustrated, because I couldn’t loop through a table using ipairs.

So, I had a table like this one:

local tbl = {}
tbl[25] = true

And I am looping though it like this:

for i, v in ipairs(tbl) do
	print(v)
end

It doesn’t print anything in the output when I loop through the table using ipairs
but when I use pairs instead of ipairs it prints the value of v in the output (true), what is going on here? Why can’t I get it to print anything when I use ipairs but when I use pairs it prints something?

EDIT: Im going to bed as its 9:14 PM for me, so I will check back here in the morning, good night

I think once you set tbl[25], it’s converted into a dictionary, even though 25 is an integer.

When looping through a table using ipairs, the loop will end if the next value is equal to nil. Meanwhile, pairs does not have this limitation. Only use ipairs on arrays when you need order to be guaranteed.

3 Likes

You can only use ipairs on arrays, use the pairs iterator.

1 Like