Crafting Script Deletes 2 Items

Hi, I created a crafting and Inventory system for my game, but I realised that when I craft something it deletes 2 of the needed item of the inventory in an indirect way, here is what I mean

this is the starter gui and where everything is located at
image
the inventory script deals with droping the items when clicked on the inventory gui and the local script is what deletes the item


this is the inventory and when you click on an item that you can pick up it goes here, and the box with the text “Text here” will be copied and specified with the item name that you picked up, like “Part” or “sword”


and this is the crafting screen it pops up when you press craft, it gives you the tool you select and deletes the item in your inventory

Video

here are the scripts

server script in serverscriptservice
local inventoryEvent = game.ReplicatedStorage.Events.InventoryEvent

game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder", Player)
	Inventory.Name = "Inventory"
	
	Inventory.ChildAdded:Connect(function(Item)
		inventoryEvent:FireClient(Player,Item.Name, true)
	end)
	inventoryEvent.OnServerEvent:Connect(function(Player, ItemName, Value)
		if Value == false then
			local SelectedItem = Player.Inventory:FindFirstChild(ItemName)
			
			if SelectedItem:IsA("Modle") then
				SelectedItem.Parent = game.Workspace
				SelectedItem:SetPrimaryPartCFrame(Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2))
			else
				SelectedItem.Parent = game.Workspace
				SelectedItem.CFrame = Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2)
			end
		end
	end)
end)
Local script in the axe craft button
script.Parent.MouseButton1Click:Connect(function()
	
	if game.Players.LocalPlayer.Inventory:FindFirstChild("Part")  then
		game.Players.LocalPlayer.Inventory:FindFirstChild("Part"):Destroy()
		script.Parent.Parent.Parent.ItemFrame:FindFirstChild("Part"):Destroy()

		local Backpack = game.Players.LocalPlayer.Backpack
		local Tool = game.ReplicatedStorage.CraftableTools.Axe
	
		wait(0.1)
		
		if Tool then
			Tool:clone().Parent = Backpack
		end
		print("Yes")
	else
		print("No")
	end
	
end)
InventoryScript in mainframe
local InventoryEvent = game.ReplicatedStorage.Events.InventoryEvent
local ItemFrame = script.Parent:FindFirstChild("ItemFrame")

InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
	if Value == true then
		local ItemButton = ItemFrame:FindFirstChild("Sample"):Clone()
		ItemButton.Visible = true
		ItemButton.Name = ItemName
		ItemButton.Text = ItemName
		ItemButton.Parent = ItemFrame
		
		ItemButton.MouseButton1Click:Connect(function()
			ItemButton:Destroy()
			InventoryEvent:FireServer(ItemName, false)
		end)
	end
end)

Basicly the problem is that when I craft a tool, it deletes it off my inventory, and when I drop the same item (another copy) It doesnt drop, and it does that to the ammount that I craft, like if I craft 5 and drop 5, the 5 of that item I drop wont drop but dissapear of my inventory

Anything will help

How exactly are you counting the items are you using int values or something on the lines of that? Is it also possible to see some of the scripts its hard too tell what to see is the problem with out them.

No not really, the items that you pick up goes in a folder in the player character
local inventoryEvent = game.ReplicatedStorage.Events.InventoryEvent


game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder", Player)
	Inventory.Name = "Inventory"
	
	Inventory.ChildAdded:Connect(function(Item)
		inventoryEvent:FireClient(Player,Item.Name, true)
	end)
	inventoryEvent.OnServerEvent:Connect(function(Player, ItemName, Value)
		if Value == false then
			local SelectedItem = Player.Inventory:FindFirstChild(ItemName)
			
			if SelectedItem:IsA("Modle") then
				SelectedItem.Parent = game.Workspace
				SelectedItem:SetPrimaryPartCFrame(Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2))
			else
				SelectedItem.Parent = game.Workspace
				SelectedItem.CFrame = Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2)
			end
		end
	end)
end)

script in serverscriptservice

Could it be that the part isn`t being teleported or maybe the game thinks the axe deleted it while you were crafting, Could you open the output and are there any errors there?

The part is being teleported because it checks if its in my inventory, but yeah it might be that its not getting deleted ill check

this part of code inside ServerScriptService has to be outside of game.Players.PlayerAdded
because every time player joins it connects another function to that event for example:

first player joins and then another one joins
now this event now has 2 connected functions
if one of them will fire the remote the function will be done twice

it says this
image

So it says that after you pick up 1 item or when you craft 1 item? If that’s after you pick up 1 then it looks like repeated 2 times and if you crafted 1 item it also looks like it`s repeated its job twice.

is it outside game.PLayers.PlayerAdded

image

Nope

It says that when I craft using a specific tem then drop a copy of the item ,like when I have 2 Parts in my inventory and use one to craft and drop the other one, the one I drop just dissapear to nowhere and says that in the output

Could be the reason?, I didn’t read the code enough. Thought that was an part.
image

this local script has to fire server and the server has to destroy the part and clone axe because everything done inside LocalScript is only visible to you

If it is a model then it will do a separate string but it is a part so it will skip to the next 4 lines after.

he meant that it is misspelled

Ohhh i didn`t even see my self

no I dont think thats why, because thats an if statement, if its a modle then it will do that but if its a part it will do the rest after else

if SelectedItem:IsA("Modle") then 
			SelectedItem.Parent = game.Workspace
			SelectedItem:SetPrimaryPartCFrame(Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2))
		else  <---- if not modle 
			SelectedItem.Parent = game.Workspace
			SelectedItem.CFrame = Player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 2)
		end

You misspelled model is what he thought was the problem i didn`t get it too.

Also I think you may have not seen vlads script