Arrays inside of Arrays?

How would I go about getting another array inside of an array without having to need a variable for it?
For example:

Knives = { {} --[[ Legendary ]] , {} --[[ Rare]] , {} --[[ Uncommon ]] , {} --[[ Common]] }

How would I go about accessing each array, without having to add

Knives = {-- Legendary = {}, }

?

The ‘variables’ are called keys, and keys always exist, even if they are numeric.
You can use this to read from your first example.

legendary = Knives[1]
uncommon = Knives[3]

Usually if I had keys, all I would have to do is…

Playerdata[Player]["Knives"]["Legendary"]

But i’m not trying to do this so…

for i, v in pairs(PlayerData) do
   Playerdata[Player]["Knives"][v[1]] -- First array in 'Knives' table
end

Would this even work, i’m not in studio right now.

I am assuming that Playerdata[Player].Knives is the Knives table?

for i, v in pairs(Playerdata[Player]["Knives"]) do
-- v = legendary table, uncommon table, etc
end

Yes, I see.

So basically I wouldn’t need to Search through playerdata I could already have that inside pairs, it could be a simplistic way of doing things.

So all I need to do is,

for i, v in pairs(Playerdata[Player]["Knives"]) do
   print(v[1], v[2], v[3], v[4]) --Prints each array sorted from legendary to common
end

to get each knife string inside the array, correct?

Nevermind, used LuaDemo to test, and this works.

1 Like