Need help with sword inventory


First is local, second is module, and third is the error.
I am very confused. It does not seem to be working as I wish.

What do you get when you print i and v?

Edit: My bad,didn’t readi t compeltely, what’s the line that’s erroring

When I print the numbers of items in the table it says 0, and I forgot the include in the picture the part where when I index the first item of the table and get it’s name it errors.

Oh that’s easy, the # operator only works on tables and strings, you’re trying to use it on a dictionary, you need a custom function for that

Something like this

local function getValueFromIndex(dictionary, index)
	local currentIndex = 0
	for _, value in pairs(dictionary)
		currentIndex += 1
		if currentIndex == index then
			return value

So you’d do getValueFromIndex(swordmodule.Swords, 1).name

could you upload a picture showing the lines numbers and the line where the error is generated?

the error is generated on the last line.

Oh thank you for pointing that out.

1 Like

I still need help with why it’s erroring.

How did you implement the system?

Elaborate please. I’m trying to make a inventory where it loads all the items in order and I have a module with all the data.

Did you try out what I had mentioned and it stil lerrors? If so, can you show me how you implemented my idea

There are two separate errors. You fixed the first one, but the second error is how when I try to index the name from the inventory module it says it doesn’t exist.

Can I see the code related to the 2nd error?

It is included. The last line in the picture I replied to the person a couple posts above.

I have no idea if this is your actual issue, but your trying to index it with [1] which does not exist in the sword table. If you need an ordered list you can create a helper function to find sword based on ids. This does however mean you have to add an id field to the swords


local function GetSword(id)
    for i, v in pairs(swords) do
       if ( == id) then
           return v

I’m confused, can’t you do “table_name”[number_index] to get a specific index in the table? It should be leading to the first item in the dictionary.

Dictionaries have no order and as a result no numerical indexes (unless you set them), you’re thinking of arrays / lists

Oh thank you so much that saved me alot of time. I wish I could mark 2 answers right since there were 2 different solutions but I’ll mark yours as a solution since it was the main problem.