Help with this array within dictionay

Hi! I’m trying to create a dictionary of data, with the item, and then the array of all of the stats of the item within it. I’m using DataStore2. I’m trying to go through the dictionary, and then loop through the array it gets. So i’m creating a folder for the item stats, and then all the values within it.

This is after i created the store, which is fine:

   	Consumables_Store:GetTable({
	["Simple Potion of Healing"] = {["Description"] = "A Potion of simple healing and stuff", ["Item_Count"] = 3, ["Strength"] = 25, ["Equipped_To_Toolbar"] = false, ["Toolbar_Binding"] = "1"};
	["Simple Potion of Energy"] = {["Description"] = "A Potion of simple Energy and stuff", ["Item_Count"] = 5, ["Strength"] = 23, ["Equipped_To_Toolbar"] = false, ["Toolbar_Binding"] = "2"};
})

  local Consumables_Store_Gotten = Consumables_Store:Get()

    for dicname, dicvalue in pairs(Consumables_Store_Gotten) do
	local Item_Folder = Instance.new("Folder")
	Item_Folder.Name = dicname
	Item_Folder.Parent = Consumables_Folder
	print(dicname)
	for statname, statvalue in next(dicvalue) do
		print(dicname, dicvalue)
		if type(statvalue) == 'number' then
			local intvalue = Instance.new("NumberValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Item_Folder
		elseif type(statvalue) == 'boolean' then
			local boolvalue = Instance.new("BoolValue")
			boolvalue.Name = statname
			boolvalue.Value = statvalue
			boolvalue.Parent =  Item_Folder
		elseif type(statvalue) == 'string' then
			local stringvalue = Instance.new("StringValue")
			stringvalue.Name = statname
			stringvalue.Value = statvalue
			stringvalue.Parent = Item_Folder
		end
	end
end

The problem is it errors when it gets to the table, and it wont loop through :*( Can anyone help?

1 Like

Let’s see…

The main problem is probably key string lengths, try shorten them down.


Minor points, here’s a simplification.

for statName, statValue in next, dicvalue do
	local ValueObject
	if type(statValue) == 'number' then
		ValueObject = Instance.new("NumberValue")
	elseif type(statValue) == 'boolean' then
		ValueObject = Instance.new("BoolValue")
	elseif type(statValue) == 'string' then
		ValueObject = Instance.new("StringValue")
	end

	if ValueObject then
		ValueObject.Name = statName
		ValueObject.Value = statValue
		ValueObject.Parent = Item_Folder
	end
end

Even better, compact.

local ValueTable = {
	["number"] = "NumberValue";
	["boolean"] = "BoolValue";
	["string"] = "StringValue";
}

for statName, statValue in next, dicvalue do
	local ValueObject = Instance.new(ValueTable[type(statValue)])
	ValueObject.Name = statName
	ValueObject.Value = statValue
	ValueObject.Parent = Item_Folder
end
2 Likes

If you replace “next(dicvalue)” on line 11 with “next, dicvalue” or “pairs(dicvalue)” your issues should be resolved. :slight_smile:

3 Likes

Thankyou Guys im slapping my forehead right now… i feel silly. DUHHH XD. Thx!!