Crafting still working without proper materials

  1. What do you want to achieve?
    I’m crafting ore into ore bars. So, I set up an Ore Collection inventory and a Metal Bar Inventory. When you click the Ore Bar Button then it deletes the amount of Ore needed and crafts a Metal Bar.

  2. What is the issue? Include screenshots / videos if possible!


    The required item that is read from a module script requires 2 copper and 1 tin to make the bar. If I have 1 Cooper and 1 tin then it still works(when it shouldn’t) and it removes the 1 tin. I want it to work when they have enough supplies and not work when they dont. I don’t know what I’m missing.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve youtube and read through the forms. I just don’t understand what I’m missing.

for i, oreAmount in pairs(barCraftingInfo[theBarName])do
		local material = forge:FindFirstChild(i)
		if material then
				
			if material.Value >= oreAmount then
				material.Value = material.Value - oreAmount
						crafted = true
				local newAmount = material.Value	
		
			else
				crafted = false

			end
		end

I suggest making a function for this as the code you have won’t seem to work (haven’t tested), here’s a base code I made but you change this to your liking.

local playerItems = {}
function CraftItem(requiredItems)
  -- Just some error handling for the required items table
  assert(type(requiredItems) ~= "table", "Please set RequiredItems to a table!")
  --[[

  Player Item Table Example:
  {Copper = 4, Tin = 5}

  Required Item Table Example:
  {Copper = 2, Tin = 1}
  ]]--

  local crafted = false
  local items_crafted = {} -- Table to check which items have been crafted

  for material, amount in pairs(requiredItems) do -- Loop Through RequiredItems table
    if playerItems[material] ~= nil then
      if playerItems[material] > amount then
        -- They have enough of the material
        items_crafted[#items_crafted + 1] = "BlahBlah" -- This can be anything, it just needs to be in the table for it to work.
        playerItems[material] = playerItems[material] - amount -- Subtract the amount from their current total.
      end
    end
  end
  crafted = (#items_crafted == #required_items) -- Check if they can craft it.
  if crafted then
    print("Crafted!")
  else
    print("Not Crafted!")
  end
end

-- Call The Function

CraftItem({Copper = 2, Tin = 1})

This is just the base code, you can adapt off of this to fit your needs.