Help with inventory system

I’m trying to reduce the lag created by an inventory system using a UIGridLayout. Inventories can have upwards of 300+ items and creating those all at once can create a lot of lag, even when I close and delete the inventory UI the lag persists.

2 Likes

simplest idea I can think of is using a “paged” format so only a maximum number of UI elements can appear at any given time

I tried this, still had the same lag, I even tried creating only the page the player wants to view

maybe ur script isnt optimised? can you show us so we can try locate the problem

i personally dont think that the UI is the issue
considering how you tried having less UI elements be shown at once, it is most likely something to do with your script, unless there is something in your UI elements that might cause the lag.

The script isn’t much, I just get the player’s inventory and use a for loop to iterate and create it

There are alot of items that are being created for an inventory item…
2 Frame, 2 ImageLabels, 1 TextLabel, 1 ViewportFrame, 1 WorldModel and 1 Camera.

are you creating the item through Instance.new() and also using it’s second parameter? If so change your script.

--old
local frame = Instance.new("Frame", player.PlayerGui.ScreenGui)

--new
local frame = Instance.new("Frame")
frame.Parent = player.PlayerGui.ScreenGui

The second parameter is known for causing lag. Avoid using it.

I am not, I use this function to create instances:

local function CreateInst(Name, Properties)
	local GenInst = Instance.new(Name);
	for k, v in pairs(Properties) do
		GenInst[k] = v;
	end;
	return GenInst;
end;

If the issue is happening when you load the inventory and it lags, you should probably try adding a task.wait()

like u said, u have over 300 frames and each of them have a WorldModel which is probably the cause…

dunno if this is similar but worth checking it out homie How to fix lag with WorldModels

I dont think it’s the WorldModels, because I used ImageLabels to display the Item before. (I have too many items in my game to get a Image of right now so this is my way around that.) The lag still persists even when I destroy the Inventory UI.

I can’t send the full script since its well over 1000+ lines, but this is the part where the GuiObjects are generated:

for key, value in pairs(PlayerInventroy) do
	local itemData = value.ItemData
	local shopModel = nil
	for _, shopItem in pairs(game.ReplicatedStorage.Content:GetDescendants()) do
		if (shopItem:FindFirstChild("ShopModel") or shopItem:FindFirstChild("ShopModelInventory")) and shopItem.Name == value.RealName then
			shopModel = shopItem:FindFirstChild("ShopModelInventory") and shopItem.ShopModelInventory:Clone() or shopItem.ShopModel:Clone()
			setupWelds(shopModel)
			break
		end
	end
	if not shopModel then
		if itemData.Class == "Gamepasses" or itemData.Class == "Roles" then
			continue
		else
			shopModel = Instance.new("Model")
			shopModel.PrimaryPart = Instance.new("Part")
			shopModel.PrimaryPart.Transparency = 1
		end
	end
	local frame = v1.CommonFunctions.GenInst("Frame", {
		ZIndex = 10,
		BackgroundColor3 = Color3.new(1, 1, 1),
		BorderSizePixel = 0,
		LayoutOrder = itemData.DisplayOrder,
		BackgroundTransparency = 1,
		Parent = l__inventorypage__103
	})
	local contentFrame = v1.CommonFunctions.GenInst("Frame", {
		Name = "Content",
		ZIndex = 10,
		BackgroundTransparency = 0,
		BackgroundColor3 = Color3.fromRGB(36, 35, 36),
		AnchorPoint = Vector2.new(0.5, 0.5),
		Active = true,
		Selectable = true,
		Size = UDim2.fromScale(0.95, 0.95),
		Position = UDim2.fromScale(0.5, 0.5),
		Parent = frame
	})
	local rarityLabel = v1.CommonFunctions.GenInst("TextLabel", {
		ZIndex = 10,
		Parent = contentFrame,
		AnchorPoint = Vector2.new(1, 0),
		Position = UDim2.fromScale(1, 0),
		BackgroundTransparency = 1,
		Text = u9[itemData.Rarity][1],
		TextColor3 = u9[itemData.Rarity][3],
		TextStrokeColor3 = u9[itemData.Rarity][2],
		TextStrokeTransparency = u9[itemData.Rarity][4] or 0,
		BorderSizePixel = 0,
		Size = UDim2.fromScale(0.72, 0.2),
		TextScaled = true,
		TextXAlignment = Enum.TextXAlignment.Right,
		TextYAlignment = Enum.TextYAlignment.Top
	})
	local viewportFrame = v1.CommonFunctions.GenInst("ViewportFrame", {
		Size = UDim2.fromScale(0.75, 0.75),
		AnchorPoint = Vector2.new(0.5, 0.5),
		Position = UDim2.fromScale(0.5, 0.5),
		BackgroundTransparency = 1,
		BackgroundColor3 = Color3.fromRGB(71, 71, 71),
		Parent = contentFrame,
		Ambient = Color3.new(1, 1, 1),
		ZIndex = 10,
		LightColor = Color3.new(1, 1, 1)
	})
	local worldModel = v1.CommonFunctions.GenInst("WorldModel", {
		Parent = viewportFrame
	})
	local camera = v1.CommonFunctions.GenInst("Camera", {
		Parent = viewportFrame
	})
	if u10[itemData.Class] then
		local classData = u10[itemData.Class]
		camera.CFrame = classData.Camera_CFrame
		shopModel:SetPrimaryPartCFrame(classData.ObjectCFrame)
		shopModel:ScaleTo(classData.Scale)
		if classData.Special and classData.Special[value.RealName] then
			local specialData = classData.Special[value.RealName]
			camera.CFrame = specialData.Camera_CFrame
			shopModel:SetPrimaryPartCFrame(specialData.ObjectCFrame)
			shopModel:ScaleTo(specialData.Scale)
		end
		shopModel.Parent = worldModel
	end
	local itemNameLabel = v1.CommonFunctions.GenInst("TextLabel", {
		Parent = frame,
		ZIndex = 10,
		Text = itemData.Name,
		Name = "ItemName",
		AnchorPoint = Vector2.new(0, 1),
		Size = UDim2.new(0.93, 0, 0.2, 0),
		TextScaled = true,
		BackgroundTransparency = 1,
		Position = UDim2.fromScale(0.045, 1),
		TextColor3 = Color3.new(1, 1, 1),
		Font = Enum.Font.SourceSansBold,
		TextYAlignment = Enum.TextYAlignment.Bottom,
		TextXAlignment = Enum.TextXAlignment.Right
	})
	local uiStroke = v1.CommonFunctions.GenInst("UIStroke", {
		Thickness = 2.5,
		Color = u8[itemData.Rarity],
		Parent = contentFrame
	})
	local button = v1.CommonFunctions.GenInst("ImageButton", {
		BackgroundTransparency = 1,
		ImageTransparency = 1,
		ZIndex = 25,
		Size = UDim2.fromScale(1, 1),
		Parent = contentFrame,
		Name = "Button"
	})
	local rarityImage = v1.CommonFunctions.GenInst("ImageLabel", {
		Size = UDim2.fromScale(0.33, 1),
		BackgroundTransparency = 1,
		Image = "rbxassetid://4938932998",
		Position = UDim2.fromScale(0, 0),
		ImageColor3 = u8[itemData.Rarity],
		ZIndex = 10,
		Parent = contentFrame
	})
	local aspectConstraint = v1.CommonFunctions.GenInst("UIAspectRatioConstraint", {
		AspectRatio = 1,
		AspectType = "FitWithinMaxSize",
		DominantAxis = "Width",
		Parent = rarityImage
	})
	viewportFrame.CurrentCamera = camera
	contentFrame:SetAttribute("Name", value.Name)
	contentFrame:SetAttribute("Rarity", value.ItemData.Rarity)
	button:SetAttribute("Key", value.ItemId)
	script.Parent.Parent.InterfaceTemplates.RarityGradients[itemData.Rarity]:Clone().Parent = contentFrame
end

Why don’t you just have a pre-made frame somewhere in ReplicatedStorage or something and then just clone it from there and edit it as you need? I don’t see the point of u making the Frame rapidly from 0

I’ll try this! I’ll let you know how it goes.

This did seem to help, another issue I’m facing is that whenever I close and delete the Inventory the fps dosen’t go back to normal, and this stacks whenever I open and close the inventory alot