Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

i,v in pairs wont iterate through a table in order when I assigned them keys?

local Tab = {Xd = 'G', ge  = 'g',ce = 'gg',keke = '1', fefe = '2'}

for i,v in pairs(Tab) do
	print(i,v)
end

will not print in order but when i remove the keys like Xd, ge, ce, keke, fefe it itereates in order cna anyone tell me why it dosen't iteate in order and i can get it to iterate in order

1 answer

pairs does not iterate in any particular order -- the keys of a table do not have an order; they are merely present.

If your data has an order, you should organize your table with that order as a sequence. Maybe something like this:

local data = {
	{"Xd", "g"},
	{"ge", "g"},
	{"ce", "gg"},
	{"keke", "1"},
	{"fefe", "2"},
}

-- note that "ipairs" and not "pairs" is used --
-- ipairs goes in order, but only over *lists*
for _, kv in ipairs(data) do
    print(kv[1], kv[2])
end

This discussion is preserved as it was. New answers, comments, and votes are closed.