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
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.
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.
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
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
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.
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.