String as indexes in a table

hello.
So im making a crafting module which returns the result with the correct ingredients. The only problem is that how do i insert string values that doesn’t have indexes, like below here

-- a way to insert "Wood" without making it like this

-- what i dont want
local ingredients = {
    [1] = "Wood",
    [2] = "Stone",
    [3] = "Wood",
}
-- what i want

local ingredients = {
    "Wood"
    "Stone"
    "Wood"
}

please send me any help as i will appreciate it

Out of curiosity, why do you need a table without indexing? I don’t even think if you can even make a table without indexes.

you won’t probably understand, but i need it so i can’t make my life harder and just using table.find() if the recipe has a ingredient that matchs the original ingredient inserted.

i don’t think i understand, is this what you mean table.insert(ingredients, "Wood")?

So basically, do you know when you have a table and want to insert an apple and a banana.

Simply, you use table.insert()

and if you wanna review the table, you can print it out.
The only problem is that the items inside the table have numerical indexes, in which case i don’t want that.

the two tables you listed in your post are the same… what indexes do you want then?

image
notice the number indexes

yeah that’s how lua tables work, you can set the index as the item instead like

[“Torch”] = true

Tables elements without a specified index will always default to a number as it’s index; you can’t work around that

1 Like

i realize now, im just gonna find another way to achieve what i want. thanks.

I don’t get why you wouldn’t want indexes. Why can’t you use table.find()?

here’s what i meant by my post above

local ingredients = {}

ingredients["Wood"] = true

print(ingredients["Wood"]) -- true (so you don't need to use table.find)

for ingredient, value in ingredients do
	print(ingredient)
end

i didnt wanna use table.find() because i thought you can make indexes be set as a string, since i can’t do that, im just gonna find the other way to do it

only problem would be that if there are 2 wood blocks, it would override, so this will not work for my likings

althought thanks for the support

instead of true you increment it as a number

[“Wood”] = 2

For this table, similar to how Roblox formats it, you could do something like this, taking to account on what bg9 said:

{
     ["Recipes"] = {
          ["Wood"] = (count)
          ["Stone"] = (count)
     },
     ["Result"] = {
          "Torch"
     }
}

You could also do this:

["Torch"] = {
     ["Wood"] = (count)
     ["Stone"] = (count)
}

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