Compare Two Tables / Help With Recipe System

Heya, I’ve been working on a cooking system where you put 3 ingredients into a table and if the ingredients align with a dish, it’ll output the dish. (Ex: You put one burger one patty and one cheese and it outputs a burger) I’ve been trying to wrap my head around how I should handle this and I’ve been stumped. My main idea is to compare two tables, one with the current ingredients, and one with all the recipes. How should I go about this?

local Recipes  = {
	["Burger"] = {"Bread", "CookedPatty", "Cheese"},
	["Soda"] = {"SodaCup, SodaLid"}
}

return Recipes
-- Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local CookingRecipes = require(ServerScriptService.Modules.CookingRecipes)
local Ingredients = script.Parent:WaitForChild("Ingredients")
local PrxoimityPrompt = script.Parent:WaitForChild("ProximityPrompt")
local MaxIngredients = 3

local CurrentIngredients = {}

-- Cook Ingredients
PrxoimityPrompt.Triggered:Connect(function(player)
	-- Put Ingredients
	if player.PlayerStats.HoldingObject.Value ~= nil then
		if #Ingredients:GetChildren() < MaxIngredients then
			local Ingredient = Instance.new("StringValue")
			Ingredient.Parent = Ingredients
			Ingredient.Name = "Ingredient"
			Ingredient.Value = player.PlayerStats.HoldingObject.Value.Name
			
			player.PlayerStats.HoldingObject.Value:Destroy()
			player.PlayerStats.HoldingObject.Value = nil	
			table.insert(CurrentIngredients, Ingredient.Value)
			print(CurrentIngredients)
		end
	end
	local TotalIngredients = Ingredients:GetChildren()
	
	if #Ingredients:GetChildren() == MaxIngredients then
		-- Output Ingredients
		
	end
end)
1 Like

Not sure if there are built-in functions but here’s one that compares tables:

function compare(t1, t2)
   for i, v in pairs(t1) do
       local find = table.find(t2, v)

       if (not find) then return false end
   end
   
   return true
end

MIGHT work, not sure. Haven’t tried to compare tables.

1 Like

If there are no extra values in the players items, you can do

for i, v in require(Recipes) do
	if require(char.items) == v, then
		cook(i)
	end
end
1 Like

This one almost works, but it’ll always return false because how I set the recipe table.

1 Like

Maybe:

for i, v in pairs(require(Recipes)) do
	local found = true
	for I, V in ipairs(v) do
		if not table.find(require(playerItems), V) then
			found = false
			break
		end
	end
	if found then
		makeItem(i) -- i is the thing to make
		break
	end
end
1 Like