Button removing multiple items from inventory instead of just one

I want my program to remove an item from a folder called “inventory” when the player clicks for that singular item to be removed from their inventory through a GUI button.
My issue is that each time I try to remove an item from the inventory the amount of items removed from the “inventory” list goes up by one. I’ve looked in the developer hub and can’t seem to find what I am looking for, maybe I have just missed it. Also, at first my code attached below had FindFirstChild instead of WaitForChild when defining SelectedItem but I was getting an error. I changed it to WaitForChild and got the infinite yield warning that’s when I realized I didn’t really understand something in the code or why it wasn’t executing properly.

local SpawnPoint = game.Workspace.SpawnPoint

local InventoryEvent = game.ReplicatedStorage.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:WaitForChild(ItemName)
			local NewInstance = Instance.new("Part")
			NewInstance.Name = ItemName
			
	
			SelectedItem.CFrame = Player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,2)
			print(SelectedItem)
			print(SelectedItem)
			SelectedItem.Transparency = 0
			SelectedItem.CanCollide = true
	

			SelectedItem.Parent = game.Workspace.ServerSide

			print("put it in")
			print(SelectedItem.Position)
			print(SpawnPoint.Position)
			wait(1)
			SelectedItem = ""

			
			
		end
		
	end)
	
end)

By the way this code is in a script under ServerScriptService and I’m a beginning Roblox scripter.

3 Likes

You should probably remove the function

InventoryEvent.OnServerEvent:Connect(function(Player, ItemName, Value)

From under the other function.

game.Players.PlayerAdded:Connect(function(Player)

And make them seperate.
Everytime a new player joined, the function “InventoryEvent” is fired again. Meaning when the RemoteEvent is fired, the script runs it twice.

3 Likes
local SpawnPoint = game.Workspace.SpawnPoint

local InventoryEvent = game.ReplicatedStorage.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:WaitForChild(ItemName)
			local NewInstance = Instance.new("Part")
			NewInstance.Name = ItemName
			
	
			SelectedItem.CFrame = Player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,2)
			print(SelectedItem)
			print(SelectedItem)
			SelectedItem.Transparency = 0
			SelectedItem.CanCollide = true
	

			SelectedItem.Parent = game.Workspace.ServerSide

			print("put it in")
			print(SelectedItem.Position)
			print(SpawnPoint.Position)
			wait(1)
			SelectedItem.Name = ""

			
			
		end
		
	end)
	
end)
1 Like

I got it to work. I don’t really understand why I had this problem but in the MouseButton1Click function was running multiple times. In that function was the FireServer command. This meant that the server was being fired multiple times. I fixed it by putting a bool so if the MouseButton1Click function had been accessed once this click it couldn’t be accessed again till another click. I don’t understand why this bug happened but I’m glad to have fixed it

2 Likes

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