How do I adjust X/Y values based on screen size

Hey there!

First off, I have a canvasGroup. Now, I have a seperate resizing script, and this script just limits the amount you can scale the CanvasGroup. Here is the script (Credit to: @KrazyGamerPlay with their “Resizable Frame” model).

local MinSizeX = 378
local MinSizeY = 271

local MaxSizeX = 968
local MaxSizeY = 694
local ObjectSize = script.Parent

ObjectSize.Changed:connect(function()
	if ObjectSize.Size.X.Offset < MinSizeX and ObjectSize.Size.Y.Offset < MinSizeY then
		ObjectSize.Size = UDim2.new(0, MinSizeX, 0, MinSizeY)
	elseif ObjectSize.Size.X.Offset < MinSizeX then
		ObjectSize.Size = UDim2.new(0, MinSizeX, 0, ObjectSize.Size.Y.Offset)
	elseif ObjectSize.Size.Y.Offset < MinSizeY then
		ObjectSize.Size = UDim2.new(0, ObjectSize.Size.X.Offset, 0, MinSizeY)
	end
	if ObjectSize.Size.X.Offset > MaxSizeX and ObjectSize.Size.Y.Offset > MaxSizeY then
		ObjectSize.Size = UDim2.new(0, MaxSizeX, 0, MaxSizeY)
	elseif ObjectSize.Size.X.Offset > MaxSizeX then
		ObjectSize.Size = UDim2.new(0, MaxSizeX, 0, ObjectSize.Size.Y.Offset)
	elseif ObjectSize.Size.Y.Offset > MaxSizeY then
		ObjectSize.Size = UDim2.new(0, ObjectSize.Size.X.Offset, 0, MaxSizeY)
	end
end)

I need MinSize X/Y and MaxSizeX/Y to change based on the players screen size if that makes any sense (if not I’ll be happy to explain further).

Anyways, Cheers!
Robyn

1 Like

Your best option here is to use scaled gui properties.

What do you mean by that, can you please elaborate?

GUI looks fine in studio but in game looks out of place This post should probably help.

That’s not the problem. I need the X/Y sizes to change based on the screen size. This is completely different to what your saying. I need to take the size of the screen, and adjust the values so it the minimum size and max size would look the same on different devices.

There are a few plugins that are meant for that such as AutoScale Lite

You quite literally just need to set the offsets to zero. UDim2.new(1, 0, 1, 0) The second and fourth digit should always be 0. You’re doing it backwards now that I’m looking at your script again. The first and third digit should never be 0 unless you want an offset which in your case, you don’t.

I’m confused, when I tried that, the frame just got super big and broke. Can you maybe provide an example?

Sorry for my lateness. Change the scale to what you need! It doesn’t have to be UDim2.new(1, 0, 1, 0). It could be UDim.new(TheScaleYouWant, 0, TheScaleYouWant, 0). I recommend playing around with the values until you figure out what scale it is that you want.

hey, if i understand your question correctly, what you need is the screens size in pixels.

local Viewport = game:GetService("Workspace").CurrentCamera.ViewportSize
print(Viewport.X, Viewport.Y)

this will give you the player’s screen size in pixels.

And how would I adjust Max/Min size X/Y to Viewport.X/Y?

Should those values to ObjectSize.Size.X.Scale instead of offset or what?

Edit: So I figured out I should have been using offset this entire time and not scale. Thank you all for your helpful comments!

Old Post

So I figured something out. That is not the problem (As of now) the problem is that I was scaling in offset in another script. Now I need to figure out how to scale in… scale. Here is a snippet of the script:

userInputService.InputChanged:Connect(function(input)
	if isResizing and input.UserInputType == Enum.UserInputType.MouseMovement then
		local currentMousePosition = userInputService:GetMouseLocation()
				local offset = currentMousePosition - initialMousePosition
				frame.Size = UDim2.new(
					initialSize.X.Scale,
					initialSize.X.Offset + offset.X,
					initialSize.Y.Scale,
					initialSize.Y.Offset + offset.Y
			)
	end
end)