Help with crafting system

Hi. So I’m working on a crafting system and is almost done with it but the problem I found was that with my script, as long as you have enough of 1 of the required materials, the craft will be completed. And since I’m a bit new with using tables, I’m not too sure how to fix this.
This is the module script:

local craftingInfo = {

["Sword1"] = {
	
	["stone"] = 15;
	["coal"] = 19;
	
};
}

return craftingInfo

And this is the serverscript where it is being crafted.

     local RS = game:GetService("ReplicatedStorage")
     local craftingInfo = require(RS:WaitForChild("CraftingInfo")) -- modulescript
     local Tools = game.ServerStorage.Tools -- location of tools

game.ReplicatedStorage.RemoteFunctions.CraftTool.OnServerInvoke = function(player, toolName)
local crafted = false

for i,v in pairs(craftingInfo[toolName]) do
	local material = player.inv:FindFirstChild(i) -- inventory folder
	if material then
		if material.Value >= v then -- this is the problem, it doesn't check if I have all the materials.
			material.Value = material.Value - v
			crafted = true
		else
			crafted = false
		end
	end
end

if crafted == true then
	local tool = Tools:FindFirstChild(toolName):Clone()
	
	if tool then
		print(toolName .. " has been crafted!")
		tool.Parent = game.ServerStorage.Inventories[player.Name] -- clone to players inventory
	end
else
	print("Tool has not been crafted, not enough materials")
	
	
end

end

The issue is the verification, if the player has enough resources of the last material then crafted will be true, which then will craft the item, what you can do is add a break after crafted = false, so whenever the player doesn’t have enough resources it will stop the loop.

for i,v in pairs(craftingInfo[toolName]) do
	local material = player.inv:FindFirstChild(i) -- inventory folder
	if material then
		if material.Value >= v then -- this is the problem, it doesn't check if I have all the materials.
			material.Value = material.Value - v
			crafted = true
		else
			crafted = false
            break
		end
	end
end
1 Like