Tweaking a "check if done then do" section

Hey all!
I’ve got a cooking script, and I need to sort out a section that needs to essentially “check all recipe combinations to see if ingredients are compatible, if so display Success, if there is no compatibility after checking all combinations then display Fail”.

Currently, the script will post the Error message if the ingredient combo fails. But if I know the ingredient combo is compatible, it will cycle through each combination displaying the error message each time until it gets to the successful one. Which is not the ideal user experience where the player is seeing an Error message for a bit before seeing the Success message. I’ve got the full script below, and I’ve marked the section where it needs tweaking. Any help is much appreciated. :slight_smile:

local recipes = {
['HamSandwich'] = {{"Ham", "Bread"}};
['CheesePizza'] = {{"Cheese", "Dough"}};


}

local failIngredients = {
['HamSandwich'] = {"FakeIng01", "FakeIng02"};
['CheesePizza'] = {"FakeIng03", "FakeIng04"};

}

function onCorrectRecipe(recipeName, packedVariadic,Plr,ResultBtn,ResultMsg,ResultFinalResult)
-- display Success Message
end	

function onFailedRecipe(Plr,ResultBtn,ResultMsg,ResultFinalResult)
-- display Fail Message
end	

local correctRecipeFound = false

CookingEvent.OnServerEvent:Connect(function(Plr, ResultBtn, ResultFinalResult, ResultMsg, ...) -- last (...) is for multiple ingredients
	if Plr.leaderstats.Coins.Value >= 10 then
		local CurrentIngredients = {...}
	
		for recipeName, thisRecipe in pairs(recipes) do
			for _, v  in pairs(thisRecipe) do
				local matches = 0
				local failed = false
				print(v)
				print(matches)
				for _, b in pairs(CurrentIngredients) do
					if table.find(v,b) and #v == #CurrentIngredients then
						print('Success Check 01')
						matches += 1	
					    elseif table.find(failIngredients[recipeName], b) then
						print('Fail Check 01')
						failed = true
			  			onFailedRecipe(Plr,ResultBtn,ResultMsg,ResultFinalResult)
                       break 
					end
				end
-- BELOW IS THE SECTION THAT NEEDS TWEAKING
				if failed then break end -- to break out of "for _, v  in pairs(thisRecipe) do"
				if matches == #v then
					correctRecipeFound = true
					onCorrectRecipe(recipeName, CurrentIngredients,Plr,ResultBtn,ResultMsg,ResultFinalResult)
					break
				else
					onFailedRecipe(Plr,ResultBtn,ResultMsg,ResultFinalResult)
				end
-- ABOVE IS THE SECTION THAT NEEDS TWEAKING
			end
			if correctRecipeFound then break 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)

You should check correctRecipeFound outside the outer for loop and if it is false, then call onFailedRecipe

1 Like

Hey! Thanks for the response! Funny thing happened, it worked! Tested an incompatible combo, gave the Fail message as needed. Tested a compatible combo, it gave the Success message without going through the Fail loop.

And then after that it no longer worked. o_o; I’m not sure why that happened at all… memory cache issue? From my prints checking to see if there are matches and recipes - print(v) - it would only check one recipe and then stop. As opposed to the first time the player tries it where it searches through all recipes, this time it only checks for the recipe combo that the player successfully made, and not the ones that come before or after. I included the revised script below:

CookingEvent.OnServerEvent:Connect(function(Plr, ResultBtn, ResultFinalResult, ResultMsg, ...) -- last (...) is for multiple ingredients
	if Plr.leaderstats.Coins.Value >= 10 then
		local CurrentIngredients = {...}
	
		for recipeName, thisRecipe in pairs(recipes) do
			for _, v  in pairs(thisRecipe) do
				local matches = 0
				local failed = false
				print(v)
				print(matches)
				for _, b in pairs(CurrentIngredients) do
					if table.find(v,b) and #v == #CurrentIngredients then
						print('Success Check 01')
						matches += 1	
					    elseif table.find(failIngredients[recipeName], b) then
						print('Fail Check 01')
						failed = true
			  			onFailedRecipe(Plr,ResultBtn,ResultMsg,ResultFinalResult)
                       break 
					end
				end

				if failed then break end
				if matches == #v then
					print('Success Check 02')
					correctRecipeFound = true
					onCorrectRecipe(recipeName, CurrentIngredients,Plr,ResultBtn,ResultMsg,ResultFinalResult)
					break
				end

			end
			if correctRecipeFound then break  end	
			end
			if correctRecipeFound == false then print('Fail Check 02') onFailedRecipe(Plr,ResultBtn,ResultMsg,ResultFinalResult)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)

correctRecipeFound should be defined inside the function. The way it is now, the first time it checks, it sets it to true, and it stays that way for the next time.

That did it! :smiley: Thank you so much!