AspectRatio of UIAspectRatio constraint

I am attempting to make a plugin that automatically adds a UIAspectRatio constraint but I am not sure how to get the AspectRatio property automatically. Is there a formula to calculate this

You divide the absolute size in X by the absolute size in Y. A frame that is twice as long as it is tall would have an aspect ratio of 2.

1 Like

What if the number is too long

I don’t really know what you mean by that, sorry.

its like 2.093582935729374294719247129319237123917293712937

then i tried entering it manually into the property and it just becomes 0

The properties window only allows you to paste up to 3 decimals, yes. Lua doesn’t have this restriction since it’s not the properties window and numbers in Lua are not bound to a certain length in decimal notation or anything.

i see, thank you!

i tried it but it keeps becoming 1

Script:

script.Parent.MouseButton1Click:connect(function()
	for _, v in ipairs(game.Selection:Get()) do 
		if v.ClassName == "ImageLabel" or v.ClassName == "ImageButton" or v.ClassName == "Frame" or v.ClassName == "TextLabel" or v.ClassName == "TextButton" or v.ClassName == "TextBox" then
		local clone = Instance.new("UIAspectRatioConstraint")
		clone.Parent = v
		local x = v.AbsoluteSize.X
		local y = v.AbsoluteSize.Y
		local ratio = x/y
clone.AspectRatio = ratio
	wait(.1)
	print("UI Scaled")
		end
		end
end)

You only need a UIAspectRatioConstraint in the main frame of the GUI
Like this
image

I know but when i made the script do the math, it keeps getting 1

Did you try printing the ratio? And if so is it also 1?

yes

I even made it print the absolute sizes before doing the math and it is accurate. But when it divides, it keeps getting “1”

You insert the aspect ratio constraint before determining the ratio. The default value of the aspect ratio is 1. The size of the object gets adjusted to be aspect ratio 1 before you determine the ratio.

Put the lines where you determine ‘ratio’ before the ‘clone.Parent = v’.

3 Likes

omg lol thank you so much

1 Like

I wanna know what property I should use from FitWithinMaxSize and ScaleWithParentSize.
This happens when i use FitWithinMaxSize
On my screen :

On another device:

Edit: Kinda fixed it by using one frame for all.

1 Like