Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Error With Tables (coming up nil) Fix to this?

Asked by
XTLR 5
6 years ago
Edited 6 years ago

i have local Items = { Mining = {} }

i can do Items.Mining its tabel: some hex stuff

but Items[1] or #Items is nil.

local ShopData = { Mining = {} }

for i = 1, #ShopData do end

it will skip because shopdata is empty

1 answer

Log in to vote
0
Answered by 6 years ago

So tables can act as arrays, or as unordered sets in lua. This means that there is a mode of some sort where things like #table and table[1] will work (though 1 can be a specific key, but I won't go into that). And another mode, which you are in, where they won't.

See this tutorial on lua for more information. What this all means is that you are setting a key in your table as mining. That is separate from the "array" mode of the table that allows you to look through indexes (numbers that are accessed like shopData[1]). It also means that #ShopData will return the number zero. That's the number of array indexes.

To loop through an _unordered set of key and value pairs _ you will want to use the pairs function.

for key, value in pairs(ShopData) do
   print(key, value)
end

That would print something similar to.

Mining {}

0
thanks, this worked XTLR 5 — 6y
0
Be sure to accept the answer. User#18718 0 — 6y
Ad

Answer this question