Inventory System Adding Items to same player no matter who picks it up

Hello! So I am making a Inventory System and it works great but I ran into an issue.

It seems that when an item is picked up, it puts it in the first player to join’s inventory, no matter who has picked it up. I am wondering why it is doing this and how I can fix it.

Here is my code:


local Players = game:GetService("Players")

local ServerStore = game:GetService("ServerStorage")

local InventoryFolder = ServerStore:WaitForChild("Inventory")

local Drops = workspace:WaitForChild("Drops")

--//Setup\\--

Players.PlayerAdded:Connect(function(plr)
	
	--Player Base Inventory
	
	local PlrInventory = Instance.new("Folder")
	PlrInventory.Parent = InventoryFolder
	
	PlrInventory.Name = plr.Name
	
	--Items in Player Hotbar
	
	local HotBarItems = Instance.new("Folder")
	HotBarItems.Parent = PlrInventory
	
	HotBarItems.Name = "HotBarItems"
	
	--Armor
	
	local Armor = Instance.new("Folder")
	Armor.Parent = PlrInventory
	
	Armor.Name = "Armor"
	
	--Items
	
	local Items = Instance.new("Folder")
	Items.Parent = PlrInventory
	
	Items.Name = "Items"
	
	--MaxItems
	
	local MaxItems = Instance.new("IntValue")
	MaxItems.Parent = Items
	
	MaxItems.Name = "MaxItems"
	
	MaxItems.Value = 10
	
	--Current Weight
	
	local CurrentWeight = Instance.new("IntValue")
	CurrentWeight.Parent = Items
	
	CurrentWeight.Name = "CurrentWeight"
	
	--//Collecting\\--
	
	for i, v in pairs(Drops:GetChildren()) do
		
		v:FindFirstChildWhichIsA("ClickDetector", true).MouseClick:Connect(function()
			
			print(plr.Name.." Attempted to pickup: "..v.Name)
			
			if CurrentWeight.Value + v:FindFirstChild("Weight").Value <= MaxItems.Value then
				
				print(plr.Name.." Has enough space and has picked up: "..v.Name)
				
				v.Parent = Items
				
				CurrentWeight.Value += v:FindFirstChild("Weight").Value
				
				print("Player Space: "..CurrentWeight.Value.."/"..MaxItems.Value)
				
			else
				
				print(plr.Name.." Does not have enough space to pick up: "..v.Name)
				
				print("Player Space: "..CurrentWeight.Value.."/"..MaxItems.Value)
				
			end
			
		end)
		
	end
	
end)

It adds the items and manages capacity just fine. The items just keep going in the same person’s inventory.

Sorry if this isn’t well explained. I don’t really know another way of explaining it

Sorry for the trouble and thanks for the help!

It’s because you assign a MouseClicked event to the ClickDetector every time a player joins. You only need one event. So move the --//Collecting\\-- loop out of the PlayerAdded event.

You can still get the player that clicked the part because it’s an argument of the event.

v:FindFirstChildWhichIsA("ClickDetector", true).MouseClick:Connect(function(plr) -- the plr here

It should work if you leave everything as it was

1 Like

Oh wow I’m an idiot! It’s working perfectly now! Thank you so so very much! :grin::tada:

1 Like