How to convert numbers to ui size

So I want to find a way to make it so that I can set the size of a UI frame with any random number.

For example, if I had a UI frame that is .5 in X-Scale and I had 500 out of 500 that would be .5 on the frame because that is 100% of the UI’s size that I want to set it to.

Another example is if I had 200 out of 500, it would be 0.2 on the UI however, if I have numbers that are greater than 500, like 400 of 870, how would I find the 0.0 - 0.5 number that would fit in the UI frame. Where 870 of 870 would be .5, 400 and 870 should be variable.

I’m not sure if this makes sense, but hopefully, somebody can help me out here.

2 Likes

If I understand you correctly (and I’m not sure I do) it would be:

200/(500*2)=.2
400/(870*2)=.23
local viewsizeX,viewsizeY = 1920,1080 -- replace them with camera.ViewportSize.X and .Y respectively
local x,y = 200,500

local scaleX,scaleY = x/viewsizeX,y/viewsizeY  
1 Like

it is not for a viewport frame, just a regular frame.
I’ll try this out editing what I was trying to do though

1 Like

it returns 217, which is not what I’m looking for. the number should not be greater then the x value

yes!! thank you this was it. a big help, I’ve bee trying to solve it all of yesterday haha, appreciate you very much.
i must ask, assume that I wanted to set the max value to something other then .5 but use the same numbers what value would I change??

*2 is the .5 part. basically it’s just x2 because you need to double .5 to make it 1.
If you wanted to change .5 to .75 or any other number the simplest method would be to divide the number with 1 (e.g. 1 / .5 = 2) so for .75 would be 1 / .75 = 1.33 so the sum would be

400/(870*1.33)=.34

so if I understand correctly, if I wanted to change.5 to 2, for instance I would divide 1 / .5 = 2, and it would be
400/(870*2) = .22

??

I think this might be the solution

no, this is incorrect. i know how to convert those I’m looking for something else. Thanks for trying though!

  • @ZombieCrunchUK has the answer I’m just waiting for a reply on my follow up to their answer.

Sorry for the late reply.
I’m still a little confused on what your trying to accomplish to be honest so I don’t know if this is exactly what your trying to do but try this:

local BaseNumber=2
local MaxNumber=870
local ChosenNumber=400

local Result=(BaseNumber/100)*(ChosenNumber/(MaxNumber/100))
Result=math.floor(Result*100)/100 -- Round the number to 2 decimal places
print(Result)
--[[
      prints 0.91
]]

ok so basically what im saying is if i have a UI that is 0.5 in X scale, I want to find what number of the 0.5 is 400 of 900 for example.

so if I wanted to change the 0.5 scale to say 1.9, id wanna find what percentage of the scale is 400 of 900.

1.9 = 100%, 0.0 = 0%

hopefully this makes sense?

I think you have a slight misunderstanding on what they were showing you.

The code they were showing, contained camera.ViewportSize.X and such.
This is not an actual viewportframe, but rather ViewportSize is a Vector2 containing the LocalPlayer’s screen size.

Now, to answer your question…

local cam = workspace.CurrentCamera
local X,Y = cam.ViewportSize.X,cam.ViewportSize.Y

function convertToScale(XSize,YSize)
	return UDim2.new(XSize/X,0,YSize/Y,0)
end

local GX,GY = 10,10 --The offset size of GUI

local newSize = convertToScale()

Did I understand your question right? This code will convert GUI Offset size to scale.

So the code I gave you above is correct for the purpose and gives the correct result:

400 of 900 = 44.4%

1.9 = 100% so 1% = 0.019

44.4% = 0.84

0.84 is what the code shows as the result when these numbers are input:

local BaseNumber=1.9
local MaxNumber=900
local ChosenNumber=400

local Result=(BaseNumber/100)*(ChosenNumber/(MaxNumber/100))
Result=math.floor(Result*100)/100 -- Round the number to 2 decimal places
print(Result)
--[[
      prints 0.84
]]
1 Like

er it does not appear like it. @ZombieCrunchUK got it. look at what they wrote as I’m not sure how else to explain it. I’m not trying sizes, I’m trying to get a percent of a frame that is two numbers.

Ok! this is perfect then. does what I need it to do. thank you so much.