Inventory system giving all players item when only one is given item??

Hello.

I made an inventory system, and everything works, but for some reason when player:GiveItem() is ran, it gives everyone the item.

Now, before you go on insulting me on how semi-efficient this system is, that’s not what i’m here for, I just want to fix this problem.



if SERVER then

	util.AddNetworkString("OpenInv")
	util.AddNetworkString("UseItem")
	util.AddNetworkString("RemoveDropItem")

	local Player = FindMetaTable("Player")

	Player.Items = {}

	Item = {}
	Item.Registered = {}

	function Item.Register(classname, model, name)
		Item.Registered[classname] = {['model'] = model, ['name'] = name or "Item"}
	end

	Item.Register("cstm_smg_mp5", "models/weapons/w_smg_mp5.mdl", "MP5")
	Item.Register("cstm_sniper_m40a1", "models/weapons/w_snip_m40a1.mdl", "M40A1")
	Item.Register("cstm_rif_cz805", "models/weapons/w_rif_cz805.mdl", "CZ-805")

	Item.Register("oc_tree", "models/props_debris/wood_chunk08b.mdl", "TREEEEES")
	Item.Register("weapon_axe", "models/props/cs_militia/axe.mdl", "Axe")

	Item.Register("test_item", "models/props/cs_militia/axe.mdl", "Axe")


	function Player:RetrieveItems()
		return self.Items
	end

	function Player:GiveItem(item, amount)
		local itement = ents.Create(item)
		local uid = table.Count(self.Items) + 1
		if not self:HasItem(item) then
			self.Items[item] = {['entity'] = itement, ['equipped'] = false, ['amount'] = amount or 1, ['uid'] = uid, ['printname'] = Item.Registered[item].name, ['model'] = Item.Registered[item].model}

		else
			self.Items[item] = {['entity'] = itement, ['equipped'] = false, ['amount'] = amount + self.Items[item].amount or 1, ['uid'] = uid, ['printname'] = Item.Registered[item].name, ['model'] = Item.Registered[item].model}
		end

	end

	function Player:SetItemAmount(item, amount)
		self.Items[item].amount = amount
	end

	function Player:HasItem(item)
		if self.Items[item] then
			return true
		else
			return false
		end
	end

	function Player:TakeItem(item, amount)

		local amount = amount or 1

		if self:HasItem(item) == true then
			if self.Items[item].amount - amount >= 0 then -- Check if you have the required amount of your item
				self.Items[item].amount = self.Items[item].amount - amount -- If so, then do the math
				if self.Items[item].amount == 0 then -- But if your amount is zero after the math,
					self.Items[item] = nil -- Remove the item table.
				end
			end
		end

	end

	function Player:UseItem(item)
		if not self.Items[item] then return end
		if self.Items[item].entity:IsWeapon() then
			self:Give(item)
			self.Items[item].equipped = true
		elseif self.Items[item].entity.OnEquip then
			self.Items[item].entity.OnEquip(self)
			self.Items[item].equipped = true
		elseif not self.Items[item].entity:IsWeapon() and not self.Items[item].entity.OnEquip then
			local itement = ents.Create(item)
			itement:SetPos(self:GetPos() + Vector(0, 0, 30))
			itement:Spawn()
		end
	end

	function Player:DequipItem(item)
		if not self.Items[item] then return end
		if self.Items[item].entity:IsWeapon() and self.Items[item].equipped == true then
			self:StripWeapon(item)
			self.Items[item].equipped = false
		elseif self.Items[item].entity.OnDequip then
			self.Items[item].entity.OnDequip()
			self.Items[item].equipped = false
		end
	end

	function OpenInv(ply)
		print(ply:Nick().. " opened their inventory!")
		net.Start("OpenInv")
			net.WriteTable(ply:RetrieveItems())
		net.Send(ply)
	end

	hook.Add("ShowTeam", "OpenGUI", OpenInv)

	net.Receive("UseItem", function()
		local ply = net.ReadEntity()
		local item = net.ReadString()
		ply:UseItem(item)
		ply:TakeItem(item)

		local itemlist = ply:RetrieveItems()
		for k,v in pairs(itemlist) do
			itemlist[k].selected = false
		end
		if itemlist[item] then
			itemlist[item].selected = true
		end
		net.Start("OpenInv")
			net.WriteTable(itemlist)
		net.Send(ply)
	end)

	net.Receive("RemoveDropItem", function()
		local ply = net.ReadEntity()
		local item = net.ReadString()

		local droppedent = ents.Create("dropped_item")
		droppedent:SetNWString("ClassName", item)
		droppedent:SetModel(ply.Items[item].model)
		droppedent:SetPos(ply:GetPos() + Vector(0, 0, 30))
		droppedent.GiveItem = item
		droppedent:SetModel(ply:RetrieveItems()[item].model)
		droppedent:Spawn()

		ply:TakeItem(item)
		local itemlist = ply:RetrieveItems()

		for k,v in pairs(itemlist) do
			itemlist[k].selected = false
		end

		if itemlist[item] then
			itemlist[item].selected = true
		end

		net.Start("OpenInv")
			net.WriteTable(itemlist)
		net.Send(ply)

	end)

end


Can you show us how you’re calling GiveItem on the player?

This inventory system was for my gamemode, but my friend wanted me to put it in his DarkRP server, so I integrated it into pocket.



function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime() + 0.2)

	if not SERVER then return end

	local ent = self.Owner:GetEyeTrace().Entity
	local canPickup, message = hook.Call("canPocket", nil, self.Owner, ent)

	if not canPickup then
		if message then DarkRP.notify(self.Owner, 1, 4, message) end
		return
	end

	if self.Owner:GetActiveWeapon().ClassName == "pocket" then -- Giving item
		if Item.Registered[ent.ClassName] then
			print(self.Owner:Nick())
			self.Owner:GiveItem(ent.ClassName, 1)
			ent:Remove()
		end

		if Item.Registered[ent.weaponclass] then
			print(self.Owner:Nick())
			self.Owner:GiveItem(ent.weaponclass, 1)
			ent:Remove()

			timer.Simple(0.2, function()
				self.Owner:StripWeapon(ent.weaponclass)
			end)
		end
	end
end