Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I convert TextBounds to UDim2.new?

So I have this TextBox. I want to make it so its size is always the same as its textbounds. I think the way to do it is to convert TextBounds to UDim2, and make it so it's size is that. But TextBounds is 2 numbers, and UDim2 is 4.

--This is what I tried
local CodeBox = script.Parent
local CodeBox_Size = script.Parent.Size
local CodeBox_TextBounds = script.Parent.TextBounds

for i=0,math.huge do --infinite loop
	wait()
	CodeBox.Size = UDim2.new(CodeBox_TextBounds)
end

1 answer

TextBounds are actually measured in pixels. The offset of UDim2 is also measured in pixels. You can make a UDim2 value, and then insert 0 for the Scale parts and put TextBounds in for the Offset parts.

Update: Didn't realize that you were changing the text. In that case, then you should be assigning your variable inside the loop. Here is the edited script:

--This is what I tried
local CodeBox = script.Parent

for i=0,math.huge do --infinite loop (Just wondering why is this here?????????????)
    wait()
    local CodeBox_Size = script.Parent.Size
    local CodeBox_TextBounds = script.Parent.TextBounds
    CodeBox.Size = UDim2.new(0, CodeBox_TextBounds.X, 0, CodeBox_TextBounds.Y)
end

3 comments

0

When I run it, the size changes to the bounds of the text. After that, when I change the text, it still stays the same size and breaks.

0

Updated. Recheck on what I said.

0

Still doesn't work, instead of expanding when I put in more text, it shrinks. Oh well, I guess I'll have to just use another method.

This discussion is preserved as it was. New answers, comments, and votes are closed.