Recipe script does not work the way it needs to, help

Sorry to make another post, but this is pretty important to me. See- this crafting recipe script only works if the recipe has two ingredients at least to make a double ingredient recipe. But I need it to work for recipes that only have a single ingredient, any reason why it’s not working?


local Storage = game:GetService("ReplicatedStorage")
local Remote = Storage:WaitForChild("PestleEvent")


local Recipes = {
	TestIngredient3 = {"Rotweed", "HarborBloom"}, -- Different Recipe
	TestIngredient1 = {"HillLily"}, -- Different Recipe
}



local function pestleCraftIngredients(Player)
	local inv = {}
	local Backpack = Player.Backpack
	
	for x, tool in pairs(Backpack:GetChildren()) do
		table.insert(inv, tool.Name)
	end
	
	if #inv < 2 then
		print("You need more items")
		
	else
		for x, tables in pairs(Recipes) do
			if table.find(tables, inv[#inv-1]) and table.find(tables, inv[#inv]) then
				for xx, toolToDelete in pairs(tables) do 
					Backpack[toolToDelete]:Destroy()
				end
				local item = Storage.MidCraftedIngredients[x]:Clone()
				item.Parent = Backpack
				break
			end
		end
	end

end



Remote.OnServerEvent:Connect(pestleCraftIngredients)

I don’t quite understand your script but this might be something worth highlighting…

1 Like

I’ve tried changing it and I’m not sure how to make it work.

i didn’t get it working but someone else did thanks

if #inv < 1 then

Before you were only proceeding with the execution of the function if the number of items in the player’s inventory was greater than or equal to 2 (not less than or equal to 1).

if #inv >= 1 then

Try doing this, it’ll allow for the function to execute providing the player has at least one recipe item in their inventory.

if #inv >= 1 then
	for x, tables in pairs(Recipes) do
		if table.find(tables, inv[#inv-1]) and table.find(tables, inv[#inv]) then
			for xx, toolToDelete in pairs(tables) do 
				Backpack[toolToDelete]:Destroy()
			end
			local item = Storage.MidCraftedIngredients[x]:Clone()
			item.Parent = Backpack
			break
		end
	end
end