Why does Roblox lag when I resize gui

Roblox started to lag after I resized a frame. (The lag is too bad to record videos). I update the size of the frame every time the screen size changes. If you need any more info please ask, I don’t really know what else to put in. It’s a scripting problem, not UI design I think. That’s why its in Scripting Support.

1 Like

We can’t help you if you don’t show us the code.

The codes messy and I used Roblox TS. But here

This is the code that find and sets the size. Its run everytime workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize") is fired

local function FindSize(Ratio, Padding)
	local _0 = Workspace.CurrentCamera
	if _0 ~= nil then
		_0 = _0.ViewportSize
	local CameraSize = _0
	if CameraSize then
		if CameraSize.X >= CameraSize.Y then
			-- X Bigger
			local Y = (CameraSize.Y - 36) - Padding * 2
			local X = Ratio * Y
			return UDim2.new(0, X, 0, Y)
			-- Y Bigger
			local X = (CameraSize.X) - Padding * 2
			local Y = X / Ratio
			return UDim2.new(0, X, 0, Y)
	return UDim2.new(0, 0, 0, 0)
local function UpdateSize(setSize)
	local Camera = Workspace.CurrentCamera
	local _0 = Camera
	if _0 ~= nil then
		_0 = _0:GetPropertyChangedSignal("ViewportSize"):Connect(function()
			local Size = FindSize(BackgroundRatio, BackgroundPadding)
			print("Updating size: " .. tostring(Size))

Again I use roblox ts and Roact so its very messy

I think its because you do not have a debounce so it could fire a-lot of times.

I thought I should put one in, but ended up not doing so cause I thought it would cause “Fake lag” which is basically just a small delay

Updating size: {0, 933}, {0, 600} (x32)
Updating size: {0, 930}, {0, 598} (x64)
Updating size: {0, 924}, {0, 594} (x128)
Updating size: {0, 910}, {0, 585} (x256) 
Updating size: {0, 893}, {0, 574} (x512)
Updating size: {0, 849}, {0, 546} (x1024)
Updating size: {0, 790}, {0, 508} (x2048)

Now with the debounce theres a weird parttern in the output. The amount of times Updating size is printed doubles. I found a solution i think but I just wanted to post this cause it looks neat

Does it lag the game or no? You can delay the time needed aswell.

I found that setting the size causes the lag, not finding it. So instead of having a boolean debounce, I set it to the current size then check if the debouce is equal to the size im changing to and if it is dont do anything. That wasnt very well worded so heres an example

local Debounce = UDim2.new(0,0,0,0)

function UpdateSize()
     local Size = FindSize() -- Finding the size dosnt cause lag
     if (Size ~= Debounce) then -- Make sure the size isnt the same
          Debounce = Size
          SetSize(Size) -- Setting the size causes lag so I want to minimize this

This works suprisingly well. I have no clue how I didnt see this before!

1 Like