I managed to fix it without having to do a reversed for loop.
Great! Make sure you don't accidentally skip elements, which was my original worry with your code.
I didn't understand what are you talking about; give me an example.
So consider something like this:
local tbl = { a = 1, b = 2, c = 3 }
local k, v = next(tbl)
print(k, v) -- prints a key and its corresponding value. On my machine this is "c" and "3"
k, v = next(tbl, k) -- Find the next value after "c".
print(k, v) -- prints "b" and "2" on my machine
k, v = next(tbl, k) -- Find the next value after "b".
print(k, v) -- prints "a" and "1" on my machine
This is exactly what pairs uses to iterate over a table. If you try:
for k, v in pairs(tbl) do
print(k, v)
end
You'll get the same "c 3", "b 2", "a 1".