How to check for multiple dictionary values in a table?

Hopefully I used the right phrasing and stuff in the title.

How would I be able to check for multiple dictionary values in a table? I can’t switch to an array because I need to check if the player has certain values’ names that correlate to the keys within the table, as well as their values matching the number defined by each key before making the script give it the “all good” (hopefully that makes sense?)

local CraftingRecipes = {
["Part3"] = {
	[1] = { --don't pay any mind to this, this will be for an alternate recipe system i will tackle in the future
		["Part1"] = 1;
		["Part2"] = 2;
	};
};

return CraftingRecipes

If you couldn’t tell by now, I’m trying to achieve a crafting system.

This is the current (mess of an) attempt I’ve made at achieving what I want in the title, but it it will print if only one of the required items is in the “Inventory” folder (which is created via a script in ServerScriptService)

for craftableItem,v1 in pairs(CraftingRecipes) do --gets all the recipes
	for i,v2 in ipairs(v1) do
		for material1,v3 in pairs(v2) do
			
			if Player.Inventory:FindFirstChild(material1) then
				print(craftableItem)
				
				for index,v4 in pairs(CraftingRecipes[craftableItem]) do
					for requiredMaterial,amount in pairs(v4) do
						if Player.Inventory:FindFirstChild(requiredMaterial) then
							if Player.Inventory:FindFirstChild(requiredMaterial).Value >= amount then
								print(requiredMaterial)
							else
								break
							end
						else
							break
						end
					end
				end
			end
		end
	end
end

Hopefully I’ve provided enough information, I’m completely and utterly stumped and it’s extremely frustrating trying a plethora of different things without ever reaching the intended result (only printing when the player has both values). The script I’ve used is intended to show what crafting recipes are available (much like Terraria’s), but any scripting help will most likely be applicable to the actual crafting system as well.

Anyways, any help will be greatly appreciated!

I would have some bool variable, say canCraftRecipe, that will hold if the player can craft the recipe. Start with this being true, and loop through all of the required materials for the recipe. Loop until either the player doesn’t have enough of some material (in which case you set canCraftRecipe to false and break the loop), or until all materials have been looped through (in which the player has sufficient materials).

Then you just need a simple if statement to do whatever action if canCraftRecipe remains as true

1 Like

Oh my god, it worked!

I tried using a Debounce like that earlier, but I guess I had it in the wrong spot.

The print fires twice, but that isn’t really a problem and is more so a slight unoptimization in my mind, but since it’s going to create a button for the actual crafting system it isn’t a huge deal. All I’ll have to do to overcome that is put a :FindFirstChild() to see if the button already exists.

All of that aside, thank you!