Crafting gives player every item that can be craftable by crafting only one

So i am currently experimenting with tables so tried making a very simple crafting system.
Unfortunately the script gives player every tool by crafting only one of them even though i set everything right(i think).

Here is my script

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local open_button = script.Parent.Open
local close_button = script.Parent.BG.Close

open_button.MouseButton1Click:Connect(function()
	script.Parent.BG.Visible = true
end)
close_button.MouseButton1Click:Connect(function()
	script.Parent.BG.Visible = false
end)

local craft_frame = script.Parent.BG.CraftFrame
local requirements_frame = craft_frame.Parent.RequirementsFrame
local recipes_frame = craft_frame.Parent.RecipesFrame
local craft_button = craft_frame.CraftButton

local Backpack = player.Backpack

local Tools = {
	Axe = {"Log", "Fiber", ReplicatedStorage.Tools.Axe};
	DiamondSword = {"Diamond", "Blackstone", ReplicatedStorage.Tools["Diamond Sword"]}
}

for _, button in pairs(recipes_frame:GetChildren()) do
	if button:IsA("TextButton") then
		button.MouseButton1Click:Connect(function()
			if button.Name == "Axe" then
				requirements_frame.Requirement1.Text = Tools.Axe[1]
				requirements_frame.Requirement2.Text = Tools.Axe[2]
				craft_button.MouseButton1Click:Connect(function()
					if Backpack:FindFirstChild(Tools.Axe[1]) and Backpack:FindFirstChild(Tools.Axe[2]) then
						Backpack:FindFirstChild(Tools.Axe[1]):Destroy()
						Backpack:FindFirstChild(Tools.Axe[2]):Destroy()
						
						Tools.Axe[3].Parent = Backpack
					end
				end)
				
			elseif button.Name == "Diamond Sword" then
				requirements_frame.Requirement1.Text = Tools.DiamondSword[1]
				requirements_frame.Requirement2.Text = Tools.DiamondSword[2]
				craft_button.MouseButton1Click:Connect(function()
					if Backpack:FindFirstChild(Tools.DiamondSword[1]) and Backpack:FindFirstChild(Tools.DiamondSword[2]) then
						Backpack:FindFirstChild(Tools.DiamondSword[1]):Destroy()
						Backpack:FindFirstChild(Tools.DiamondSword[2]):Destroy()

						Tools.DiamondSword[3].Parent = Backpack
					end
				end)
			end
			end)
	end
end
1 Like

The problem here is that you’re binding the MouseButton1Click event on the craft button to every single recipe at the same time, meaning you’re firing an event for every single recipe when crafting something.

To fix this i recommend making a variable that stores the current selected recipe, and when you click the craft button, it uses the current selected one.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.