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

Why does this not print in order when I do a i loop?

local Tab = {}

for i = 1,1000 do
	Tab['G'..i] = 'CGI'
end

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

Im getting random things like it just wont be in order

G518 CGI G485 CGI G729 CGI G11 CGI G87 CGI G395 CGI G24 CGI G694 CGI G720 CGI G198 CGI G10 CGI G548 CGI G442 CGI G245 CGI G765 CGI G272 CGI G686 CGI G710 CGI G9 CGI G463 CGI G650 CGI G61 CGI G795 CGI G107 CGI G453 CGI G437 CGI G856 CGI G699 CGI G644 CGI G432 CGI G218 CGI G42 CGI G385 CGI G866 CGI G232 CGI G133 CGI G754 CGI G423 CGI G594 CGI G52 CGI G429 CGI G416 CGI G473 CGI G820 CGI G167 CGI G104 CGI G372 CGI G337 CGI G738 CGI G796 CGI G810 CGI G8 CGI G71 CGI G157 CGI G386 CGI G392 CGI G108 CGI G390 CGI G695 CGI G753 CGI G944 CGI G190 CGI G926 CGI G445 CGI G319 CGI G369 CGI G718 CGI G685 CGI G17 CGI G494 CGI G348 CGI G362 CGI

1 comment

0

where are the items in tab? also, pairs is not ordered

1 answer

Lua Tables have an ordered Array part, which you index with consecutive positive integers (starting with 1), and a Dictionary part where the keys can be other types, in your case strings.

If you did this:

local Array = {}

for i = 1,1000 do
    Array [i] = 'CGI'
end

wait(2)

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

Things would print in order, because ipairs is an ordered iterator, whereas pairs is an unordered looping over dictionary keys. Note that for this to work as expected, the array needs to have non-nil values for all consecutive indices.

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