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

Why Aren't The Indexes Of The Table Being Added?

Asked by 8 years ago
Edited 8 years ago

So what I'm trying to do is get a table to save in a data store. But in order to do that, I must save X amount of values in the table AND THEN save that table in the data store, but for some reason the table has 0 items in it. Here's the script: (P.S. I skimmed the script to the most important parts. Every variable has a value and everything is double checked to work right; The only problem seems to be that the children don't get added to the table so I'm not sure what's wrong with it.)

local inventoryToSave = {}

for _,category in pairs(inventory:GetChildren()) do
    for _,item in pairs(category:GetChildren()) do
        local file = {} -- I'm adding a table as a value
        file[1] = category.Name
        file[2] = item.Name
        file[3] = item.Amount.Value
        inventoryToSave[itemname] = file -- The index for the tables is the name of the item
    end
end

print(#inventoryToSave) -- This prints 0, meaning that the items aren't going through
dataStore:SetAsync("ExampleKey", inventoryToSave) -- Here is where I send the table to be saved

I've tried taking off the 'local' part but it still doesn't go through and I really am stuck on this. If anyone could please help I would be so grateful. Thank you!

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

You are trying to get a length of a dictionary, not an array. This means that using the symbol "#" and table.getn will return 0, because those were designed to work with arrays. To bypass this, try writing a function like the following , which returns all the number of keys (standard or not) in a table:

local function GetLen (t) local n = 0; for _,_ in pairs(t) do n = n + 1; end return n; end

0
Code block please. At least inline code. GoldenPhysics 474 — 8y
Ad

Answer this question