How would I approach saving crafting recipes in a module script?

Hi, I’m making a crafting system and I’m saving the crafting recipes inside a module script. However, I feel like the module script is not good and it can only store 3 materials (wood, stone and iron) and if I want to add a new material to the game I have to add the material to the crafting of every item even if it doesn’t need it otherwise the script won’t work.

can you lead me to the right way?

local Craftable = {}


Craftable.Items = {
		["Sword"] = {
			["Name"] = "Sword",
			["Wood"] = 1,
			["Stone"] = 1,
			["Iron"] = 0
        
		},
		["Gun"] = {
			["Name"] = "Gun",
			["Wood"] = 2,
			["Stone"] = 2,
			["Iron"] = 0
		},
		["Pickaxe"] = {
			["Name"] = "Pickaxe",
			["Wood"] = 1,
			["Stone"] = 1,
			["Iron"] = 0
		},
		["Axe"] = {
			["Name"] = "Axe",
			["Wood"] = 1,
			["Stone"] = 1,
			["Iron"] = 0	
	},
	["Steel"] = {}
1 Like

You could create an array in the dictionary, effectively saying something like

local Craftable = {}

Craftable.Items = {
       ["Sword"] = {
       ["Name"] = "Sword",
       ["Ingredients"] = {["Wood"] = 1, ["Stone"] = 2, ["Iron"] = 3}
       }
}

All ingredients is is an index, the value would still be the array.

You could then use a for loop to separate the values, so something like

for ind,val in pairs (Craftable.Items.Ingredients) do -- ind is the name of the ingredient, val is the amount required
      
end

You could do something with this to help you achieve.

There may be some better ways, I’ve just woken up and this is what I think would work.

4 Likes

This looks great But may I ask how can I make the script read the name of the table instead of the “Name”?

1 Like

The table is indexed as Sword. So you could just say

Craftable.Items.Sword

That would give you access to that item and the name. If you wanted to return the name of the sword you would do

print(Craftable.Items.Sword.Name)

That would return “Sword”

2 Likes