Looking for help inserting tables into tables

Need help inserting a table from a local function into an event’s table. Hard for me to briefly explain.

local function GetItemData(itemData)
	
	local itemPack = {
		["Item"] = {
			["ItemID"] = 0;
			["Config"] = {
				["Identity"] = {};
				["Modifications"] = {};
				["Stats"] = {};
			};
		};
		["Empty"] = true;
	};
	
	itemPack.Item.ItemID = itemData.Item.ItemID.Value;
	itemPack.Empty = itemData.Empty.Value;
	
	-- Identity
	for index, attribute in ipairs(itemData.Item.Config.Identity:GetChildren()) do
		
		if (attribute.ClassName == "ImageLabel") then
			
			local attributePack = {
				[attribute.Name] = {
					["ImageID"] = attribute.Image;
					["ImageColor3"] = tostring(attribute.ImageColor3);
				};
			};
			
			table.insert(itemPack.Item.Config.Identity, attributePack);
			
		else
			
			local attributePack = {
				[attribute.Name] = attribute.Value;
			}
			
			table.insert(itemPack.Item.Config.Identity, attributePack);
			
		end
		
	end
	
	return itemPack
	
end

-- Public Methods

function SaveInventory(player)

	local playerUserID = PlayersService:GetUserIdFromNameAsync(player.Name)
	local itemInventoryStorage = DataStoreService:GetDataStore(playerInventoryDataName);
	
	local inventory = PlayersService:FindFirstChild(player.Name).PlayerData.CharacterData.Inventory
	local newInventory = CreateInventoryPack(inventory);

	-- Set data store key
	local setSuccess, errorMessage = pcall(function()
		
		itemInventoryStorage:SetAsync(playerUserID.."/Inventory", newInventory)

	end)
	if not setSuccess then
		print("Saving Inventory Unsuccessful...");
		warn(errorMessage);
	else
		print("Inventory Saved!");
	end

end

function CreateInventoryPack(inventory)

	local inventoryPack = {
		["Equipped"] = {
			["Equipment"] = {};
			["Tools"] = {};
		};
		["Stored"] = {
			["Equipment"] = {};
			["Quest"] = {};
			["Resources"] = {};
			["Tools"] = {};
		};
	};
	
	-- Equipped
	local items = inventory.Equipped.Equipment:GetChildren()
	for index, item in ipairs(items) do -- Equipment
		
		table.insert(inventoryPack.Equipped.Equipment, GetItemData(item));
		
	end
	
	print(inventoryPack)
	return inventoryPack;

end

Apologies for uploading so much, but I can’t really tell what I should and shouldn’t show y’know.

I’m running into an issue where [1] shows up instead of [string : tableObject.Name] or whatever–every time I try to create a new list using a local function it returns with the index instead of the name.

{
  ["Equipped"] =  ▶ {...},
  ["Stored"] =  ▼  {
     ["Equipment"] =  ▼  {
        [1] =  ▼  {
           ["Empty"] = true,
           ["Item"] =  ▼  {
              ["Config"] =  ▼  {
                 ["Identity"] =  ▼  {
                    [1] =  ▼  {
                       ["ClassType"] = ""
                    },
                    [2] =  ▶ {...},
                    [3] =  ▶ {...},
                    [4] =  ▶ {...},
                    [5] =  ▶ {...},
                    [6] =  ▶ {...}
                 },
                 ["Modifications"] =  ▶ {...},
                 ["Stats"] =  ▶ {...}
              },
              ["ItemID"] = 0
           }
        },
        [2] =  ▶ {...},
        [3] =  ▶ {...},
        [4] =  ▶ {...},
        [5] =  ▶ {...},
        [6] =  ▶ {...},
        [7] =  ▶ {...},
        [8] =  ▶ {...},
        [9] =  ▶ {...},
        [10] =  ▶ {...},
        [11] =  ▶ {...},
        [12] =  ▶ {...},
        [13] =  ▶ {...},
        [14] =  ▶ {...},
        [15] =  ▶ {...},
        [16] =  ▶ {...},
        [17] =  ▶ {...},
        [18] =  ▶ {...},
        [19] =  ▶ {...},
        [20] =  ▶ {...},
        [21] =  ▶ {...},
        [22] =  ▶ {...},
        [23] =  ▶ {...},
        [24] =  ▶ {...},
        [25] =  ▶ {...},
        [26] =  ▶ {...},
        [27] =  ▶ {...},
        [28] =  ▶ {...},
        [29] =  ▶ {...},
        [30] =  ▶ {...}
     },
     ["Quest"] =  ▶ {...},
     ["Resources"] =  ▶ {...},
     ["Tools"] =  ▶ {...}
}

How would I be able to fix this so that the first [1] to [9] would instead be [01] to [09] like their names, and the second [1] would instead be [ClassType].

Any help would be greatly appreciated!

Please let me know if I should recreate the concept with a simplified example!

3 Likes

If I’m understanding what you’re asking correctly, when you use table.insert, it adds the data to the table assuming you want it as an array. This numerically indexes it, and since there is no other indexed values in the table, starts at 1.

Instead of using table.insert, it seems you want to use it as a dictionary instead. In that case, you would just set inventoryPack.Equipped.Equipment[key] = GetItemData(item) instead. The key in this case just being whatever string you want to index the data by.

1 Like

Apologies for getting back so late, but I’m sure as you know it solved the problem. Thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.