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.)
01 | local inventoryToSave = { } |
02 |
03 | for _,category in pairs (inventory:GetChildren()) do |
04 | for _,item in pairs (category:GetChildren()) do |
05 | local file = { } -- I'm adding a table as a value |
06 | file [ 1 ] = category.Name |
07 | file [ 2 ] = item.Name |
08 | file [ 3 ] = item.Amount.Value |
09 | inventoryToSave [ itemname ] = file -- The index for the tables is the name of the item |
10 | end |
11 | end |
12 |
13 | print (#inventoryToSave) -- This prints 0, meaning that the items aren't going through |
14 | 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!
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