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

How to print a table in order?

Hello, I made this table and I want to print it in order, how would I do this?

local tableThing = {
hello = 'Hello',
hi = 'Hi',
hai = 'Hai'}

This is just an example. I attempted this but it printed them randomly, not in order.

for i, child in pairs(tableThing) do
print(child)
end

1 comment

0

I'm no good with tables aha

1 answer

You don't. Dictionaries are unordered sets, unlike normal arrays, which have indexes. Without another function to order your dictionary, it won't do it automatically. I would just not use a dictionary in your case.

local t = {'hi', 'hello'}
print ( table.concat(t, ', ') )

or

local t = {
    {name = 'hi', othervalue = 1}, 
    {name = 'hello', othervalue = 'string'}
}

table.insert(t, {name = 'harry potter', othervalue = true})

for i,v in pairs(t) do 
    print(v.name)
end

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