Error in inventory system

i’m working on a inventory system and i was following BloxyHDD Video tutorial and i ran into an Error that is confusing

External Media

if i drop 2 items and pick one of them, somehow the value glitches and gives me more then 1 and gives an error about the gui that i clicked and it clearly doesn’t have any connection between them

but if i drop 1 item and pick it up it works fine

Gui Script
local rep = game:GetService("ReplicatedStorage")
local Button = script.Parent

Button.MouseButton1Click:Connect(function()
	rep.remotes.InventorySystem.DropItem:FireServer(Button.Name)
end)
Server Script
local CollectionService = game:GetService("CollectionService")
local SSS = game:GetService("ServerScriptService")
local rep = game:GetService("ReplicatedStorage")

local manager = require(SSS.PlayerData.Manager)
local itemManager = require(SSS.PlayerData.Manager.ItemModule)

local items = CollectionService:GetTagged("Item")
local remotes = rep.remotes

local PickUpCD = {}
local DropCD = {}

for i,item in workspace.DroppedItems:GetChildren() do
	item.ProximityPrompt.Triggered:Connect(function(player)
		if table.find(PickUpCD,player) then return end
		table.insert(PickUpCD,player)
		task.delay(5,function()
			table.remove(PickUpCD,table.find(PickUpCD,player))
		end)
		print("triggered")
		item.ProximityPrompt:Destroy()
		for i,v in item:GetChildren() do
			print(v)
		end
		itemManager.AddItem(player,item.Name, 1)
		item:Destroy()
	end)
end

workspace.DroppedItems.ChildAdded:Connect(function(child)
	for i,item in workspace.DroppedItems:GetChildren() do
		item.ProximityPrompt.Triggered:Connect(function(player)
			local ItemName = item.Name
			item:Destroy()
			itemManager.AddItem(player,ItemName, 1)
		end)
	end
end)

1 Like

Make sure each item has one Triggered event handler.

soo do i put a script in each item?

No, you should check for duplicates and ensure each item has only one Triggered event handler.

It seems to happen because you’re connecting the triggered event to all dropped items every time someone drops something.

workspace.DroppedItems.ChildAdded:Connect(function(child)
	for i,item in workspace.DroppedItems:GetChildren() do --> This for loop does that
		item.ProximityPrompt.Triggered:Connect(function(player)
			local ItemName = item.Name
			item:Destroy()
			itemManager.AddItem(player,ItemName, 1)
		end)
	end
end)

So instead of doing that you should only connect “Triggered” to the recent dropped item:

workspace.DroppedItems.ChildAdded:Connect(function(child)
	child.ProximityPrompt.Triggered:Connect(function(player)
		local ItemName = child.Name
		child:Destroy()
		itemManager.AddItem(player,ItemName, 1)
	end)
end)

damn bro it did work thank you soo much!

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