#Why does a table return nil in a module script

10 messages · Page 1 of 1 (latest)

zenith eagle
#

When I print rooms, it returns a table

dawn sail
#

if you print out #rooms then it will be 0

zenith eagle
#

Yes

dawn sail
#

because you are using a key-based system (e.g. 'TopMiddle' and 'TopLeft' are keys)

#

it's not a numerical array where you can just do rooms[math.random(#rooms)]

zenith eagle
#

Oh okay

#

So how would I call a random key?

dawn sail
#

well i would utilize a for loop and assign each key into a table, where you can pick randomly from that table

#

example

local crazyThing = {
    Chicken = {},
    Dog = {},
    Cat = {},
    Dragon = {},
    Mouse = {},
}

local selections = {}
for key,_ in pairs(crazyThing) do
    table.insert(selections, key)
end
print(selections[math.random(#selections)])
zenith eagle
#

Oh okay