Parts duplicating when trying to drop them from custom inventory system

Ok, so I have an inventory system, it pratically works fine, but there is a bug that I found out when I tested it with my friends that when you drop the item there is a chance it duplicates once or more, I have no idea how that happens cause that bug didn’t happen for me but for my friend and it almost always happened when he tried to drop all items in the inventory very fast (Little side note: The ping of my friend was relatively high).

-- Local script:

local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local InventoryPlayer = player:WaitForChild("Inventory")
local template = game:GetService("ReplicatedStorage"):WaitForChild("Template")
local RF = RS:WaitForChild("RemoveItemFromInventory")








--// Events







local function handleAddition(child)
	if child then
	local templateClone = template:Clone()
	templateClone.Parent = player.PlayerGui.InventoryGUI.Frame
	templateClone.Name = child.Name
	templateClone.TextLabel.Text = child.Name
	templateClone.TextButton.Text = child.Name.." x"..child.Value
	child:GetPropertyChangedSignal("Value"):Connect(function()
		templateClone.TextButton.Text = child.Name.." x"..child.Value
		end)
		
		templateClone.TextButton.MouseButton1Click:Connect(function()
			local nameOfItem = templateClone.Name
			local item = game:GetService("ReplicatedStorage").Items:FindFirstChild(nameOfItem) 
			if item then
				  local result = RF:InvokeServer(item)
				if result == "removeGUI" then
				templateClone:Destroy()
				end
			end
		end)
	end
end



InventoryPlayer.ChildAdded:Connect(handleAddition)


-- Normal script: 


local RS = game:GetService("ReplicatedStorage")
local RF = RS:WaitForChild("RemoveItemFromInventory")




local function removeItemFromInventory(player, item)
					local cloneItem = item:Clone()
				cloneItem.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -1)
				cloneItem.Parent = workspace
			if player.Inventory:FindFirstChild(item.Name) then
				if player.Inventory[item.Name].Value > 1 then
			player.Inventory[item.Name].Value = player.Inventory[item.Name].Value - 1			
				else
			player.Inventory[item.Name]:Destroy()
			return "removeGUI"
			         end
			    end
          end






RF.OnServerInvoke = removeItemFromInventory```

maybe try adding cooldown on when removing item

But I want the players to be able to pick the items up without cooldown, do you have any idea on how to fix it without adding a cooldown?

It’s a little bit hard to understand the code, it would be very helpful if you tidy up the code.

Ok, give me a minute and I will fix it.

Ok, I think I fixed it a bit I am not that good at that sorry.

It seems like there is no debounce in your script. That would most likely fix your issue. Debounce is basically a cooldown if you don’t know.

1 Like

Is there a way to fix it without debounce, cause I want the players to take the items without having a cooldown.

It doesn’t need to be a long cooldown, just something to prevent the game from repeating the task. Something like a wait(.25) would even be enough. I’m sure you could go lower if you experimented.

Ok, I will try that out with my friend and see if it fixed the problem and if it fixed it, I will mark your post as solution.

1 Like

Ok, it is fixed, thank you. :slight_smile: