Inventory making weird spam glitch

So I have an inventory with a GUI. My problem I’m having is that whenever the player goes to make an interaction with another item to pick it up or equip it, the item gets spammed very quickly in the player’s inventory but then goes away. I believe the reason it goes away is because the script knows it’s in the player’s inventory datatable, but it’s not supposed to show it since the player has it equipped. I just wanted to know why it’s doing this weird bug.

Here’s a link of the problem:

Here’s the code I have:

function Update()
	if not PlayerData then
		return
	end
	for itemID,v in pairs(PlayerData.Inventory) do
		local itemConfiguration = ItemsModule:GetItemFromID(itemID)
	
		if not itemConfiguration then
			continue
		end
		
		--**[[ THIS IS FOR CREATING INVENTORY ITEM IN SCROLLING FRAME FROM PLAYER INVENTORY DATA
		local InventoryItem = script.Parent.InventoryWindow.LeftFrame.ScrollingFrame:FindFirstChild(itemID)
		if not InventoryItem then
			print("Item Made")
			InventoryItem = script.TemplateItem:Clone()
			InventoryItem.Name = itemID
			
			--print(itemConfiguration)
			--print(itemConfiguration.Title)
			for i,v in pairs(itemConfiguration.Title) do
				InventoryItem.Title[i] = v
			end
			
			InventoryItem.Parent = PlayerInventory.LeftFrame.ScrollingFrame
		end
		
		for indexbro,valuebro in pairs(itemConfiguration) do
			if indexbro == 'Description' then
				InventoryItem.Description.Text = valuebro;
			elseif indexbro == 'Image' then
				InventoryItem.ItemSquare.Icon.Image = valuebro;
			elseif indexbro == 'ImageSizeScale' then
				InventoryItem.ItemSquare.Icon.Size = valuebro;
			end
		end
		
		--**[[ THIS IS FOR RIGHT CLICKING INVENTORY ITEM TO BRING UP ITEM-SPECIFIC ACTIONS.
		InventoryItem.MouseButton2Click:Connect(function()
			if ItemSelected == false then
				local itemType = ItemsModule:GetTypeFromID(InventoryItem.Name);
				
				if itemType == 'Shield' then
					local itemID = tostring(InventoryItem);
					InventoryItem.ItemSquare.Equip1.Visible = true;
					InventoryItem.ItemSquare.Drop.Visible = true;
					InventoryItem.ItemSquare.Drop:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), "Out", "Quad", 0.001, false);
					InventoryItem.ItemSquare.Equip1.Text = 'Equip';
					
					clickItem(itemID);
					
				elseif itemType == 'Weapon' then
					local itemID = tostring(InventoryItem);
					InventoryItem.ItemSquare.Equip1.Visible = true;
					InventoryItem.ItemSquare.Equip2.Visible = true;
					InventoryItem.ItemSquare.Drop.Visible = true;
					InventoryItem.ItemSquare.Equip2:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), "Out", "Quad", 0.001, false);
					InventoryItem.ItemSquare.Drop:TweenPosition(UDim2.new(0.5, 0, 0.73, 0), "Out", "Quad", 0.001, false);
					InventoryItem.ItemSquare.Equip1.Text = 'Equip';

					clickItem(itemID);
					
				elseif itemType == 'Resource' then
					
				elseif itemType == 'Cape' then
					local itemID = tostring(InventoryItem);
					InventoryItem.ItemSquare.Equip1.Visible = true;
					InventoryItem.ItemSquare.Drop.Visible = true;
					InventoryItem.ItemSquare.Drop:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), "Out", "Quad", 0.001, false);
					InventoryItem.ItemSquare.Equip1.Text = 'Equip';
					
					clickItem(itemID);
					
				elseif itemType == 'Tool' then
					local itemID = tostring(InventoryItem);
					InventoryItem.ItemSquare.Equip1.Visible = true;
					InventoryItem.ItemSquare.Drop.Visible = true;
					InventoryItem.ItemSquare.Drop:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), "Out", "Quad", 0.001, false);
					InventoryItem.ItemSquare.Equip1.Text = 'Equip';
					
					clickItem(itemID);
				end
			end
		end)

		InventoryItem.MouseLeave:Connect(function()
			if ItemSelected == false then
				InventoryItem.ItemSquare.Equip1.Visible = false
				InventoryItem.ItemSquare.Equip2.Visible = false
				InventoryItem.ItemSquare.Drop.Visible = false
			end
		end)
		
		InventoryItem.Quantity.Text = v
		table.insert(inventoryIDCache, itemID)
	end
	
	local Constraint = PlayerInventory.LeftFrame.ScrollingFrame.UIListLayout
	PlayerInventory.LeftFrame.ScrollingFrame.CanvasSize = UDim2.new(0,Constraint.AbsoluteContentSize.X,0,Constraint.AbsoluteContentSize.Y)
end

--** [[REMOVING/DROPPING ITEMS FROM EQUIP SLOTS
for i,v in pairs(script.Parent.InventoryWindow.RightFrame:GetChildren()) do
	if v:IsA('ImageButton') then
		if v.Name == 'ClothingSlot' or v.Name == 'HelmetSlot' or v.Name == 'ShieldSlot' or v.Name == 'Key1Slot' or v.Name == 'Key2Slot' or v.Name == 'CapeSlot' then
			local dropbButton = v:WaitForChild('Drop');
			local removeButton = v:WaitForChild('Remove');
			local item

			local reset = 1;
			local dude = false;

			removeButton.Parent.ChildAdded:Connect(function(model1)
				print(model1);
				item = model1;
			end)
			--local item = removeButton.Parent.ViewportFrame.
			v.MouseButton2Click:Connect(function()
				if item then
					dropbButton.Visible = true;
					removeButton.Visible = true;
				end
			end)
			v.MouseLeave:Connect(function()
				dropbButton.Visible = false;
				removeButton.Visible = false;
			end)
			dropbButton.MouseButton1Click:Connect(function()
				local viewItem;
				for i,v in pairs(dropbButton.Parent.ViewportFrame:GetChildren()) do
					if v:IsA('Model') then
						viewItem = v;
					end
				end
				print(viewItem);
				DropItemRemote:FireServer(viewItem.Name);
				local equipitem = Player.Character:WaitForChild(viewItem.Name);
				equipitem:Destroy();
				viewItem:Destroy();
			end)

			removeButton.MouseButton1Click:Connect(function()
				if not dude then
					dude = true;
					print(item)
					local itemID = tostring(item);
					--local equippedItem = Player.Character:WaitForChild(item);
					--print(equippedItem);
					print(v)
					local iteminSlot = v:FindFirstChild(itemID);
					
					iteminSlot:Destroy();
					print(itemID)
					if Player.PlayerGui.ScreenGui.KeySlot1.ItemCircle:FindFirstChild(itemID) then
						Player.PlayerGui.ScreenGui.KeySlot1.ItemCircle:FindFirstChild(itemID):Destroy()
					elseif Player.PlayerGui.ScreenGui.KeySlot2.ItemCircle:FindFirstChild(itemID) then
						Player.PlayerGui.ScreenGui.KeySlot2.ItemCircle:FindFirstChild(itemID):Destroy()
					end
					--need to remove it from the player's backpack
					if Player.Backpack:FindFirstChild(itemID) then
						Player.Backpack:FindFirstChild(itemID):Destroy();
					end
					RemoveSlottedItemRemote:FireServer(itemID); --removing it from the player's character
					Update()
					removeButton.Visible = false;
					dropbButton.Visible = false;
					wait(reset);
					dude = false;
				end
				--need to destroy it from player character
				--need to see if when we equip an item, is it still inside the player data. if not we need to make it so that it is so that when they leave the game the item will be equipped when they come back.
			end)
		end
	end
end

task.defer(function()
	local oldDataJSON = ''
	while task.wait(0.0001) do		
		PlayerData = GetDataRemote:InvokeServer()
		local newPlayerData = PlayerData and HTTPService:JSONEncode(PlayerData)
		if newPlayerData ~= oldDataJSON then --If player picks up an item for example.
			oldDataJSON  = newPlayerData
			task.defer(Update)
		end	
	end
end)

RunService.RenderStepped:Connect(function()
	if Player.Character then
		local playerData = GetDataRemote:InvokeServer()
		if playerData then
			for i,v in pairs(script.Parent.InventoryWindow.LeftFrame.ScrollingFrame:GetChildren()) do
				if v:IsA('ImageButton') then
					local item = v.Name
					if playerData.Inventory[item] <= 0 then
						v:Destroy()
					end
				end
			end
		end
	end
end)

DataHandler Code:

Players.PlayerAdded:Connect(function(playerNew)
	playerNew.CharacterAdded:Connect(function(characterNew)
		if playerNew then
			local PlayerModule = require(game.StarterPlayer.StarterPlayerScripts.Player);
			PlayerModule:GivePlayersItems(playerNew);
		end
	end)
end)

GetDataRemote.OnServerInvoke = function(Player, Yield)
	local profile = DataService:GetPlayerProfile(Player, false)
	if profile then
		return profile.Data
	end
	return nil
end

EquipItemRemote.OnServerEvent:Connect(function(Player, CurrentItemID, slotNum, quantity)
	print(CurrentItemID,slotNum)
	local itemType = ItemsModule:GetTypeFromID(CurrentItemID)
	local itemID = tostring(CurrentItemID)
	local playerProfile = DataService:GetPlayerProfile(Player)
	local gameItemsArray = game.ReplicatedFirst:WaitForChild('GameItems'):GetChildren()
	
	for i,v in pairs(gameItemsArray) do
		if v.ItemType.Value == 'Shield' and v.Name == CurrentItemID then
			local clonedItem = v:Clone()
			clonedItem.Parent = Player.Character
			
			local newWeld = Instance.new('Weld')
			newWeld.Parent = clonedItem.ArmPart
			newWeld.Part0 = Player.Character["Left Arm"]
			newWeld.Part1 = clonedItem.ArmPart
			
			if playerProfile.Data.Inventory[CurrentItemID] then
				if playerProfile.Data.Equipped[CurrentItemID] then
					playerProfile.Data.Equipped[CurrentItemID] += 1
				else
					playerProfile.Data.Equipped[CurrentItemID] = 1
				end
				
				playerProfile.Data.Inventory[CurrentItemID] -= 1
			end
			
			--playerProfile.Data.Inventory[itemID] = playerProfile.Data.Inventory[itemID] - 1;
		end
		if v.ItemType.Value == 'Weapon' and v.Name == CurrentItemID then
			if slotNum == 1 then
				local clonedItem = v:Clone();
				clonedItem.Parent = Player.Character;

				local newWeld = Instance.new('Weld')
				newWeld.Parent = clonedItem.Weldy

				newWeld.Part0 = Player.Character["Left Leg"]
				newWeld.Part1 = clonedItem.Weldy
				newWeld.C0 = CFrame.new(-.5, .25, 0);
				
				--need to send the tool version to the player's backpack
				for i,v in pairs(game.ReplicatedFirst.GameTools:GetChildren()) do
					print(i,v);
					if v.Name == CurrentItemID then
						print('t');
						local Tool = v:Clone();
						Tool.Parent = Player.Backpack;
					end
				end

			elseif slotNum == 2 then
				local clonedItem = v:Clone();
				clonedItem.Parent = Player.Character;

				local newWeld = Instance.new('Weld')
				newWeld.Parent = clonedItem.Weldy

				newWeld.Part0 = Player.Character["Right Leg"]
				newWeld.Part1 = clonedItem.Weldy
				newWeld.C0 = CFrame.new(.5, .25, 0);
			end
			
			if playerProfile.Data.Inventory[CurrentItemID] then
				if playerProfile.Data.Equipped[CurrentItemID] then
					playerProfile.Data.Equipped[CurrentItemID] += 1
				else
					playerProfile.Data.Equipped[CurrentItemID] = 1
				end

				playerProfile.Data.Inventory[CurrentItemID] -= 1
			end
			
			--playerProfile.Data.Inventory[itemID] = playerProfile.Data.Inventory[itemID] - 1;
		end
		if v.ItemType.Value == 'Resource' and v.Name == CurrentItemID then
			print('you cant do that bro.')
		end
		if v.ItemType.Value == 'Cape' and v.Name == CurrentItemID then
			local clonedItem = v:Clone();
			clonedItem.Parent = Player.Character
			
			local newWeld = Instance.new('Weld');
			newWeld.Parent = clonedItem.Middle;
			
			newWeld.Part0 = Player.Character.Torso;
			newWeld.Part1 = clonedItem.Middle;
			newWeld.C0 = CFrame.new(0, -0.07, 0) * CFrame.Angles(0, math.rad(180), 0);
			
			if playerProfile.Data.Inventory[CurrentItemID] then
				if playerProfile.Data.Equipped[CurrentItemID] then
					playerProfile.Data.Equipped[CurrentItemID] += 1
				else
					playerProfile.Data.Equipped[CurrentItemID] = 1
				end

				playerProfile.Data.Inventory[CurrentItemID] -= 1
			end
			--playerProfile.Data.Inventory[itemID] = playerProfile.Data.Inventory[itemID] - 1;
		end
		if v.ItemType.Value == 'Tool' and v.Name == CurrentItemID then
			print(CurrentItemID)
			print(slotNum)
			if slotNum == 1 then
				local clonedItem = v:Clone();
				print(clonedItem)
				clonedItem.Parent = Player.Character;

				local newWeld = Instance.new('Weld')
				newWeld.Name = 'LegWeld'
				newWeld.Parent = clonedItem.Weldy

				newWeld.Part0 = Player.Character["Left Leg"]
				newWeld.Part1 = clonedItem.Weldy
				newWeld.C0 = CFrame.new(-.5, .25, 0);

				--need to send the tool version to the player's backpack
				for i,v in pairs(game.ReplicatedFirst.GameTools:GetChildren()) do
					print(i,v);
					if v.Name == CurrentItemID then
						print('t');
						local Tool = v:Clone();
						Tool.Parent = Player.Backpack;
					end
				end

			elseif slotNum == 2 then
				local clonedItem = v:Clone();
				clonedItem.Parent = Player.Character;

				local newWeld = Instance.new('Weld')
				newWeld.Parent = clonedItem.Weldy

				newWeld.Part0 = Player.Character["Right Leg"]
				newWeld.Part1 = clonedItem.Weldy
				newWeld.C0 = CFrame.new(.5, .25, 0);
			end
			
			if playerProfile.Data.Inventory[CurrentItemID] then
				if playerProfile.Data.Equipped[CurrentItemID] then
					playerProfile.Data.Equipped[CurrentItemID] += 1
				else
					playerProfile.Data.Equipped[CurrentItemID] = 1
				end

				playerProfile.Data.Inventory[CurrentItemID] -= 1
			end

			--playerProfile.Data.Inventory[itemID] = playerProfile.Data.Inventory[itemID] - 1;
		end
		if v.ItemType.Value == 'Clothing' and v.Name == CurrentItemID then
			local clonedItem = v:Clone();
			clonedItem.Parent = Player.Character;
			
			--now we need to set the id's of the player's current shirt and pants to the id's of the currentItemID's shirt nad pants
			if playerProfile.Data.Inventory[CurrentItemID] then
				if playerProfile.Data.Equipped[CurrentItemID] then
					playerProfile.Data.Equipped[CurrentItemID] += 1
				else
					playerProfile.Data.Equipped[CurrentItemID] = 1
				end

				playerProfile.Data.Inventory[CurrentItemID] -= 1
			end
		end
	end
end)