Best way to properly script this table

Hi,

I hope I can explain this properly. I’ve got this cooking script I’ve made, that at its core is a “Item01” + “Item02” = “Item03”, where the player selects the two items, and upon pressing a button then Item03 is created. I’m trying to avoid a few hundred lines of:

if Item01 or Item02 == 'PeanutButter' and Item01 or Item02== 'Bread' 
	then Plr.Stats[Item01].Value = Plr.Stats[Item01].Value -1 
         Plr.Stats[Item02].Value = Plr.Stats[Item02].Value -1 
	local Result = 'Sandwich' 
    local FinalResultImage = 'xxxxxx' 
	       Plr.Stats[Result].Value = Plr.Stats[Result].Value +1
	       ResultFinalResult.Image = 'rbxassetid://'..FinalResultImage 
	       ResultMsg.Text = 'You have created: '..Result ..'!' 
	       ResultBtn.Visible = true 
wait(5) 
           ResultBtn.Visible = false 
end

I’m not very well versed on tables, or arrays, but I’d like to set up to where if Item01 is A,B,C,D, and Item02 is E,F,G,H,I then item03 will be J. I’ve read the wiki links on tables and arrays, and readily admit I am quite confused by them. Would it be something like this?

local ComboA = {"Bread","PeanutButter","Jelly"}		
local ComboB = {"Bread","PeanutButter",}
local ResultAB = 'Sandwich'		
		
if Item01 == ComboA and Item02 == ComboB then 
         Plr.Stats[Item01].Value = Plr.Stats[Item01].Value -1 
         Plr.Stats[Item02].Value = Plr.Stats[Item02].Value -1 
              local CraftedResult = ResultAB 
              local FinalResultImage = 'XXXXXX' 
                     Plr.Stats[CraftedResult].Value = Plr.Stats[CraftedResult].Value +1 
                     ResultFinalResult.Image = 'rbxassetid://'..FinalResultImage 
                               ResultMsg.Text = 'You have created: '..ResultAB..'!'
                           ResultBtn.Visible = true wait(5) 
              ResultBtn.Visible = false
 end

Thoughts on the best way to achieve this? Thank you!

1 Like

(sorry last time didn’t work lol, heres a better version)

use a larger table that includes all combinations and then see if the current player’s combination is classified as that:
e.g.

local PeanutButterJelly = {
     {"Bread","PeanutButter","Jelly"};
     {"Bread","PeanutButter",};
}

-- player makes sandwich
local CurrentIngredients = {"Bread", "Jelly", "PeanutButter"};

for _, v in pairs(PeanutButterJelly) do
     local matches = 0
     for _, b in pairs(CurrentIngredients) do
          if table.find(v, b) and #v == #CurrentIngredients then
               matches += 1
          end
     end
     if matches == #v then
          print("made pbj!")
          break
     end
end
2 Likes

Thank you for the script help! What you’ve provided does work! If I can get more of your time, how can I adapt this for the script to check multiple recipes? I’ve included the full remote event script I have below for reference with notes/questions. I want to make sure I’m understanding this fully. :slight_smile: Thank you again!

CookingEvent.OnServerEvent:Connect(function(Plr,Item01,Item02,ResultBtn, ResultFinalResult,ResultMsg)
	if Plr.leaderstats.Coins.Value >= 10 then
		Plr.leaderstats.Coins.Value = Plr.leaderstats.Coins.Value - 10
		
		local Sandwich = {
			{"Bread","PeanutButter","Jelly","Ham"}	
		}
-- Additional Recipe example combinations
		local Pie = {
			{"Crust","Blueberry","Apple","Strawberry"}	
		}
		
		local CheesePizza = {
			{"Dough","Cheese"}	
		}
		local Hamburger = {
			{"HamburgerBun","Beef"}	
		}
		
		local CurrentIngredients = {Item01,Item02}
		
		for _, v in pairs(Sandwich) do -- I know I can't have "Sandwich" here if I want it to check multiple recipes. 
			local matches = 0
			for _, b in pairs(CurrentIngredients) do
				if table.find(v,b) and #v == #CurrentIngredients then
					matches += 1					
				end
			end
			if matches == #v then
				local CookedResult = 'Sandwich' -- Would the answer be 'v' ?
				Plr.Stats[Item01].Value = Plr.Stats[Item01].Value -1
				Plr.Stats[Item02].Value = Plr.Stats[Item02].Value -1
				Plr.Stats[CookedResult].Value = Plr.Stats[CookedResult].Value +1
				ResultMsg.Text = 'You have Cooked: '..CookedResult..'!' 
				ResultBtn.Visible = true 
				wait(5) 
				ResultBtn.Visible = false
				print("Recipe Made")
				break
			end				
		end		
	end
	    	
	if Plr.leaderstats.Coins.Value <= 10 then	
		ResultBtn.Visible = true
		ResultMsg.Text = 'You do not have enough Coins to Cook.'
		wait(3)
		ResultBtn.Visible = false
	end
	
end)

ok try this out and tell me how it goes:

local recipes = {
	Sandwich = {
		{"Bread", "PeanutButter", "Jelly", "Ham"}	
	};
	Sandwich2 = {
		{"Bread", "Jelly", "Ham"}	
	};
	Pie = {
		{"Crust", "Blueberry", "Apple", "Strawberry"}	
	};
	CheesePizza = {
		{"Dough", "Cheese"}	
	};
	Hamburger = {
		{"HamburgerBun", "Beef"}	
	};
}

function onCorrectRecipe(recipeName, packedVariadic)
	print("Recipe Made: " .. recipeName)
	for _, ingredientName in pairs(packedVariadic) do
		Plr.Stats[ingredientName].Value -= 1
	end
	Plr.Stats[recipeName].Value += 1

	ResultMsg.Text = 'You have Cooked: '..recipeName..'!' 
	ResultBtn.Visible = true 
	wait(5) 
	ResultBtn.Visible = false
end

CookingEvent.OnServerEvent:Connect(function(Plr, ResultBtn, ResultFinalResult, ResultMsg, ...) -- last (...) is for multiple ingredients
	if Plr.leaderstats.Coins.Value >= 10 then
		Plr.leaderstats.Coins.Value -= 10

		local CurrentIngredients = {...}
		for recipeName, thisRecipe in pairs(recipes) do -- I know I can't have "Sandwich" here if I want it to check multiple recipes. 
			for _, v  in pairs(thisRecipe) do
				local matches = 0
				for _, b in pairs(CurrentIngredients) do
					if table.find(v,b) and #v == #CurrentIngredients then
						matches += 1	
					end
				end
				if matches == #v then
					onCorrectRecipe(recipeName, CurrentIngredients)
					break
				end
			end		
		end
		if Plr.leaderstats.Coins.Value <= 10 then	
			ResultBtn.Visible = true
			ResultMsg.Text = 'You do not have enough Coins to Cook.'
			wait(3)
			ResultBtn.Visible = false
		end
	end
end)
1 Like

Thank you so much!!! :slight_smile: This looks fantastic!

1 Like