Problem with inventory/equipment system

I’m making an inventory/equipment system for my game. The way it works is every player has a ModuleScript stored in their player named “Inventory,” there are 24 values in there, or 24 inventory slots, and each value is set to nil by default. When an item is added to the player’s inventory, it loops through the inventory ModuleScript looking for the lowest slot that is equal to nil, and when it finds one, it sets that slot to be equal to the item’s ModuleScript, stored in ReplicatedStorage. The item’s ModuleScript stores information about the item, such as it’s name, itemType, etc. Now, when the inventory is updated by the server, it fires a RemoteEvent to the client telling it which item it just put into which slot, and the client updates it’s own version of the inventory ModuleScript accordingly.

The player has an inventory GUI, which has 24 buttons in it. These buttons are stored in a table. When the player clicks one of the buttons, the script finds that button’s position in the table, and uses that to find out which inventory slot the player is trying to access. Then, it checks if that inventory slot has something in it. If it does, a menu pops up with two options “equip” and “info.” when the player clicks the equip button, the script checks if the item is an equippable itemType (weapon or armor) if it is, then it sends a RemoteEvent to the server with the parameters (item, invSlot). This is where the problem is.

The problem is that the way I’m detecting when the inventory slot buttons are pressed is by using a for loop. If I click on an item in my inventory, and then click on another one without clicking equip on the previously clicked on item, and then I click equip on the item I just selected, it will try to equip both items. I know this is because of the loop because I put a print command right after the client sends the equip RemoteEvent, and it’s printing multiple times. So far I’ve tried putting a break in the loop right after the inventory button is clicked, but that causes the entire script to stop working after I click on an inventory button. Not sure what else to try.

Here’s the LocalScript for the inventory:

function GetChildrenOfClass(parent, class)
	children = {}
	for i, v in pairs(parent:GetChildren()) do
		if (v:IsA(class)) then
			table.insert(children, v)
		end
	end
	return children
end

wait(1)

local player = game.Players.LocalPlayer
local inventoryModule = player:WaitForChild("Inventory")
local playerInfoModule = player:WaitForChild("PlayerInfo")

local buttonClickSound = game.Workspace.ButtonClick
local inventory = require(inventoryModule)
local playerInfo = require(playerInfoModule)
local inventoryChangeEvent = game.ReplicatedStorage.InventoryChangeEvent
local gearEquipEvent = game.ReplicatedStorage.GearEquipEvent
local invButtons = GetChildrenOfClass(script.Parent.ScrollingFrame, "ImageButton")
local itemMenuFrame = script.Parent.Parent.ItemMenuFrame

for i, v in pairs(invButtons) do
	if inventory[i] ~= nil then
		v.Image = require(inventory[i]).image
	end
	
	v.MouseButton1Click:Connect(function()
		buttonClickSound:Play()
		if inventory[i] ~= nil then
			itemMenuFrame.Visible = true
			local mouse = player:GetMouse()
			itemMenuFrame.Position = UDim2.new(0, mouse.X + 20, 0, mouse.Y - 40)
			if require(inventory[i]).itemType ~= "Weapon" or require(inventory[i]).itemType ~= "Armor" then
				itemMenuFrame.EquipButton.BackgroundColor3 = Color3.new(0.490196, 0.490196, 0.490196)
			elseif require(inventory[i]).itemType == "Weapon" or require(inventory[i]).itemType == "Armor" then
				itemMenuFrame.EquipButton.BackgroundColor3 = Color3.new(255, 255, 255)
			end
			itemMenuFrame.EquipButton.MouseButton1Click:Connect(function()
				itemMenuFrame.Visible = false
				if require(inventory[i]).itemType == "Weapon" or require(inventory[i]).itemType == "Armor" then
					local item = inventory[i]
					local invSlot = i
					gearEquipEvent:FireServer(item, invSlot)
					print("equipped")
				end
			end)
		end
	end)
-- this is where I put a break
end

inventoryChangeEvent.OnClientEvent:Connect(function(item, invSlot, operation)
	if operation == "add" then
		inventory[invSlot] = item
		invButtons[invSlot].Image = require(item).image
	elseif operation == "remove" then
		inventory[invSlot] = nil
		invButtons[invSlot].Image = "rbxassetid://7128316949"
	end
end)

Try disconnecting the EquipButton function whenever a new inventory slot is clicked.

Thank you very much, that worked.