I dont know what im doing wrong (dictionarys)

local Store = {Trails = {Bubbles = {Name = "Bubbles", Cost = 1000}}, Outfits = {Name = "BusinessMan", Cost = 250}}
local Trails = (Store["Trails"])
print(#Trails)

it Outputs 0

its says theres nothing in the list Trails. I Dont know if im missing something super obvious but ive been trying to work it out for a while. i dont use dictionarys much lol

1 Like

The unary length operator # only gets the length of a table’s array part. Your table only has a dictionary part therefore its length is 0.

If you need to get how many key-value pairs there are, then use this helper function:

local function len(t)
    local n = 0

    for _ in pairs(t) do
        n = n + 1
    end
    return n
end
6 Likes

Thanks, didnt know #List doesnt include dictionarys lol

One more question tho, how do you access a list in a list?

In your example you can just use syntax like Store.Trails.Bubbles.Name. In any case you can use the normal table access (e.g. board[1][1]).

local Store = {Trails = {Bubbles = {Name = "Bubbles", Cost = 1000}}, Outfits = {Name = "BusinessMan", Cost = 250}}
local Trails = (Store["Trails"])

local function len(t)
    local n = 0

    for _ in pairs(t) do
        n = n + 1
    end
    return n
end

for i = 1,len(Trails) do
print("In for loop")
print(Trails[i])
end

im trying to make a code that cycles through the trails locating the other dictionarys. but Trails[i] returns nil

The index i (any numbers actually) aren’t defined in your dictionary. To iterate over a dictionary you have to use pairs, similarly to how the solution post did it.

1 Like