How can you convert offset size to scale size?

Hello. I have a textlabel that will adapt its size to any text length. However the code that I have to do this does this in offset. How could I convert that to scale? My code looks like this:

script.Parent.Text = game.Players.LocalPlayer.Name
script.Parent.Size = UDim2.new(0, (script.Parent.TextBounds.X+5),0,100)
--Convert to scale

17 Likes

This plugin should help you with that.

6 Likes

Scale is a % of the viewport size. You can get the size of the entire viewport from properties of the camera object and multiply by your scale values, which will give offset values.

2 Likes

You wouldn’t. The box needs to scale to the text, and the text is always some specific number of pixels wide. The text most likely scales with screen size, so if you’re trying to make things work on different screen sizes, in this case it’s okay to use offset / pixels.

2 Likes

Would you mind giving me a sample of that emporormicah? I am having trouble understanding

1 Like

To be clear, I need to convert OFFSET → SCALE

1 Like
local function ScaleToOffset(Scale)
	local ViewPortSize = workspace.Camera.ViewportSize
	return ({ViewPortSize.X * Scale[1],ViewPortSize.Y * Scale[2]})
end


local function OffsetToScale(Offset)
	local ViewPortSize = workspace.Camera.ViewportSize
	return ({Offset[1] / ViewPortSize.X, Offset[2] / ViewPortSize.Y})
end
104 Likes

Thank you for the help. This works.

3 Likes

what is supposed to be Offset[2]?

the code seems outdated? Pretty much scale is UDim2.Height and offset is UDim2.Width
Anyways I’ve fixed it because I needed the functions, here ya go

local function scaleToOffset(scale)
	local viewPortSize = camera.ViewportSize

	return {scale.Offset * viewPortSize.X, scale.Scale * viewPortSize.Y}
end

local function offsetToScale(width)
	local viewPortSize = camera.ViewportSize

	return {width.Offset / viewPortSize.X, width.Scale / viewPortSize.Y}
end
8 Likes

Offset[2] is just referencing the y position of the offset. A Udim2 position has Scale and Offset properties, each with an x and a y sub-property. This is to say, Offset[2] is referencing the second element, or y (the first is x) of any given object’s position in pixels.

2 Likes
function OffsetToScale(offset)
	local ViewPortSize = workspace.Camera.ViewportSize
	return Vector2.new(offset.X/ViewPortSize.X, offset.Y/ViewPortSize.Y)
end

If anyone needs this in the future (probably no one will but just in case), here is a modified OffsetToScale version that takes a Vector2 as input and returns a Vector2 as output.

7 Likes

ty this helped me

character limit

3 Likes