How to "stack" things in a gui inventroy

Made this simple gui inventory/blacksmith thing. It shows you all the ores you have in your inventory by making a new text button every time and putting it in a frame with grid layout. But in my game you will have a lot of the same ore at a time and the layout is only so big, so how would I make it so that the same ores stack in some way?

the button making thing:

for _, Ore in pairs(Backpack) do
		if Ore:IsA("Tool") then
			if Ore.Name:match("Ore") then
				
				local Button = Instance.new("TextButton")
				local Name = string.split(Ore.Name," ")
				Button.Parent = Ores
				Button.Name = Ore.Name
				Button.Text = Name[1]
				Button.TextScaled = true
				Button.Font = "GrenzeGotisch"
				
				Button.MouseButton1Click:Connect(function()
					local Middle = Forgery.CustomBlade.Middle
					local Edge = Forgery.CustomBlade.Edge

                          Middle.BrickColor = Ore:FindFirstChildOfClass("Part").BrickColor
						   Edge.BrickColor = Middle.BrickColor
	
				   SelectedOre = Ore.Name
					BladeColor = Middle.BrickColor
					
				end)
				
			end
		end
	end
1 Like

I’d condense it to be iron ore x 3 or something instead of a ton of buttons saying Iron ore. I’ve added the logic to your script and I apologize in advance if errors occur as I’m not sure how the rest of your script works or your GUI structure or naming. But the elseif should run if the button has already been created and should add a number of ores to the end of the text name thing. Here’s my janky solution

for _, Ore in pairs(Backpack) do
	if Ore:IsA("Tool") then
		if Ore.Name:match("Ore") and not table.find(Ores:GetChildren(), Ore.Name) then

			local Button = Instance.new("TextButton")
			local Name = string.split(Ore.Name," ")
			Button.Parent = Ores
			Button.Name = Ore.Name
			Button.Text = Name[1]
			Button.TextScaled = true
			Button.Font = "GrenzeGotisch"

			Button.MouseButton1Click:Connect(function()
				local Middle = Forgery.CustomBlade.Middle
				local Edge = Forgery.CustomBlade.Edge

				Middle.BrickColor = Ore:FindFirstChildOfClass("Part").BrickColor
				Edge.BrickColor = Middle.BrickColor

				SelectedOre = Ore.Name
				BladeColor = Middle.BrickColor

			end)
		elseif Ore.Name:match("Ore") and table.find(Ores:GetChildren(), Ore.Name) then
			local Button = Ores:FindFirstChild(string.split(Ore.Name, " ")[2]) --hopefully will find the pre existing ore button
			local text = string.split(Button.Text, " ") --splitting assuming your text is formatted like "Iron Ore"
			if #text < 4 then
				table.insert(text, " x "..tostring(2))
				Button.Text = table.concat(text, " ")
			elseif #text >= 4 then
				text[4] = tostring((tonumber(text[4]) + 1)) -- if im doing this right position 4 should be the number with the splitting at the spaces
				Button.Text = table.concat(text, " ")
			end
		end
	end
end