How would i make a better inventory gui?

So, i need to make an inventory gui. The script ive made so far lowers fps a LOT once you get to ~5 pets, im assuming its because the pets gui is binded to renderstep and always updating, and i can’t change colors / make a multidelete because the frames are constantly getting removed and replaced. How would i make a better inventory? This is my code right now:

local player = game.Players.LocalPlayer
local template = script.PetSlotTemplate
local runservice = game:GetService("RunService")
function RemoveOlds()
	for a, x in pairs(script.Parent:GetChildren()) do
		if x:IsA("TextButton") then
			if 1+1~=3 then--x.Appearance.BackgroundTransparency==1 then
				x:Destroy()
			end
		end
	end
end
script.Parent.PetSlotTemplate:Destroy()
function Main()
	if player ~= nil then
		local pets = {}
		for _, v in pairs(player.Pets:GetChildren()) do
			local reptded = 0
			repeat
				if v.Value ~= 0 then
					table.insert(pets,tostring(v))
					reptded = reptded + 1
				end
			until reptded >= v.Value
		end
		if #pets ~= 0 then
			RemoveOlds()
			for _, v in pairs(pets) do
				local clone = template:Clone()
				clone.Parent = script.Parent
				clone.Name = v.."Frame"
				local thepet
				for a, x in pairs(game.ReplicatedStorage.HatchablePets:GetDescendants()) do
					if x.Name == v then
						thepet = x
					end
				end
				if thepet ~= nil then
					clone.Icon.PetImageIcon.Image = thepet.Config.Image.Value
				else
					print(tostring(thepet))
				end
			end
		end
	end
	local chi = #script.Parent:GetChildren()-2
	script.Parent.CanvasSize = UDim2.new(0,0,0,(script.Parent.UIGridLayout.AbsoluteContentSize.Y+15))
end

runservice:BindToRenderStep("PetsInventoryUi",Enum.RenderPriority.Camera.Value+1,function()
	Main()
end)

(also, because im using render step, when you click it sometimes doesn’t work)

1 Like

Why constanly update the frames? Just update them only when a change is applied to a pet

1 Like

I use int values to keep track of the pets, so im not quite sure how ide do that…

Can you show how you’re keep track of them please, and how you apply changes to them? You could just put them in a table.

And it doesn’t really matter how you keep track of them, just make it so when a change is applied, call Main() which will reload the frames.

Im just gonna re-write it again, its really poorly made now that i think about it

1 Like

i rewrote it like you said, and it works perfectly now, tysm!

1 Like

No problem!