Help with duplicated item display

Hi. I made an inventory gui and asked my friend to find bugs. He discovered this problem when he spam opened the inventory

the inventory is displaying multiple guis of the same item when in reality the player only has one of each.

Heres my code:

		
		--stock inventory
		local playerinv = game.ReplicatedStorage.Remotes.GetSpinInv:InvokeServer()
		
		for i,v in pairs(itemholder:GetChildren()) do
			if v:IsA("ImageLabel") or v:IsA("TextButton") then
				v:Destroy()
			end
		end
		
		for i,v in pairs(playerinv) do
			local iteminfo = require(game.ReplicatedStorage.Spinners[v])
			
			local button = Instance.new("TextButton")
			button.BackgroundTransparency = 1
			button.TextTransparency = 1
			button.Text = "Banana"
			button.ZIndex = 3
			button.LayoutOrder = iteminfo.Order
			local roundframe = game.ReplicatedStorage.Extras.RoundFrame:Clone()
			roundframe.Size = UDim2.new(1,0,1,0)
			
			if v == player.Stats.EquippedSpinner.Value then
				roundframe.ImageColor3 = ownedcolor
			else
				roundframe.ImageColor3 = normalcolor
			end
			roundframe.Parent = button
			
			local imagedis = Instance.new("ImageLabel", roundframe)
			imagedis.BackgroundTransparency = 1
			imagedis.Size = UDim2.new(1,0,1,0)
			imagedis.Image = iteminfo.Texture
			
			button.Parent = itemholder
		end
		
		
		-- setting up gridlayout
		local totalpadding = gridlayout.CellPadding.X.Scale * (maxperline - 1)
		local actualScale = (1 - totalpadding) / maxperline
		
		local sizeX = itemholder.AbsoluteSize.X * actualScale
		gridlayout.CellSize = UDim2.new(0, sizeX, 0, sizeX)
		
		itemholder.CanvasSize = UDim2.new(0, 0, 0, gridlayout.AbsoluteContentSize.Y)

if you see any variables that werent indexed in the code, they were probably indexed at the beginning of the localscript

This function will fire everytime the player:

  • earns EXP from weapons (which the player gets everytime they get a hit with the gun)
  • changes their gun/spinner
  • opens the inventory GUI
  • changes the inventory mode (Weapons/Spinners)

Is there a way to make sure the gui doesnt display the same item twice?

Have you checked if the duplicated items are in the array, returned by the server, multiple times?

If players can own duplicated items but you only want it to display once in the inventory than you could easily fix this on the client by indexing the itemholder instance to check if an instance with the item name already exists.

for i,v in pairs(playerinv) do
    if not itemholder:FindFirstChild(v) then -- Check if an instance with the same item name already exists in the ``itemholder``
	local iteminfo = require(game.ReplicatedStorage.Spinners[v])
			
	local button = Instance.new("TextButton")
        button.Name = v -- Assign the item button name to be equal to the item name

        ....

     end
end   
2 Likes

This seems to work as intended! Thank you!

1 Like