How to print arrays/keys from this table I created?

This is my table inside a module (item list table, with different categories):

local biglist = {
	
	Items = {
		{
			Category = "Items";
			Name = "Test Item";
			Type = "Sword";
			Marketable = false;
			MarketValue = 0;
		};
	},
	
	Consumables = {
		{
			Category = "Consumables";
			Name = "Vigilant Ash";
			Type = "Consumable";
			Marketable = false;
			MarketValue = 0;
		};
	}
	
}

return biglist

This is how i’m checking and receiving items right now (and it is not working well):

local function retrieveItemFromData(item)
	for _,v in pairs(itemsData) do
		print(_,v) -- outputs below
		for i,x in pairs(v) do
			if x.Name == item then
				return x
			else
				warn(item.." does not exist in the game data, please check for typos.")
			end
			--break
		end
	end
end

Calling 2 or more items starts to cause weird behaviors and outputs:

	local item = retrieveItemFromData("Test Item")
	local item2 = retrieveItemFromData("Vigilant Ash")

-- output
  Items table: 0xea88d181b69bbf00 (x2)
  15:38:37.770 - Vigilant Ash does not exist in the game data, please check for typos.
  Consumables table: 0x395d92130f2c1680
  Test Item 784F0284-50A2-4DDB-8C8C-C927AA12F56D
New tag editor version coming online; unloading the old version (x2)

First line prints the Items table twice, for some reason while the Consumables table is called once and it also says Vigilant Ash does not exist which is and else statement on the function that retrieves the items. Now I definitely did something wrong with this and I need help.

Every time i’m working with tables like this i’m getting confused and messed up :cry:

Well what exactly is “itemsData”, you have 2 tables and each of them have their own values.

itemsData = require(reps:WaitForChild("ItemList")), biglist is supposed to be a table containing categories, within each category there are items.

What exactly is “ItemList” then?

Oh, it’s just the name of the modulescript that i’m requiring:

It contains:

Tables arent bad at all, its more the approach you’ve taken to using them. It may be more effient to do it this way, but it will just make things more complicated. Keep it clear and simple when it comes to tables. Tables are all organised values, so if you are organised using them should actually make life a lot easier.

Seems like my table is quite organized, with 2 different categories and 1 item in each category, and what I am trying to approach is checking if an item exists within all the categories.

1 Like

The output is right. You warn if it’s not in the first table you check, rather than if it’s not in any table. Remember that the code in a for loop runs individually for every key-value pair it loops through.

Edit: Basically:

The first time around, it goes into Items, finds Test Item, and returns. Only Items is printed since it didn’t need to check consumables.

The second time around, it goes into Items again, doesn’t find the Ash, and warns. It then checks the Consumables and finds the Ash, and returns.

3 Likes

multiple dictionary’s put into a table like that is hectic.

At the moment you have a table inside a table inside a table. which if you keep things clear shouldnt be a problem.

this tend to make it clearer how it all fits inside one another.
Heres how I normally put multiple tables together(Makes things Easy To Understand)

ilocal item1 = {}
local item2 = {}
local Dctionary1 = {Item1}
local Dictionary2 = {Item2}


local DictionaryList = {Dictionary1 , Dictionary2}
1 Like

This makes sense, a lot of sense to be exact, so how can I check if it exists in all of the tables?

1 Like

Is not that I dont understand it, its just more complicated then it needs to be.

Return when you find it, (like you do now) and after the loop you can assume it wasn’t found, since return stops the loop.

3 Likes

So where can I fit the warning output? Sorry I am really trying hard to understand xD
I have two loops, putting it after the second loop still warns even if both items exist.

local function getDataFromItemName(item)
	for _,v in pairs(itemsData) do
		for catName, cat in pairs(v) do
			for _, i in pairs(cat) do
				if i.Name == item then
					return i
				end
			end
		end
	end
	warn(item.." does not exist in the game data, please check for typos.")
end
2 Likes

Oh, this is where I would insert the warn, however why do you add another

for _, i in pairs(cat)?

2 loops are enough to get within my item, it works perfectly right now :wink:

1 Like

Anyways with this I finally have a better, proper inventory system:

Thanks to you guys helping me out with this one issue :slight_smile:

You do not need more than two loops if you explicitly index with Items or Consumables in your code, or if you explicitly index the first table you get from the module. However, using a third loop ensures that no matter how many tables coexist, the function will always catch everything. So if for example, you added a third table to your module, or a third category, having three loops makes it not a problem, as opposed to having only two, where you have to edit the code again.

1 Like