"Advanced Textwrapping"

Hello. I’m currently trying to make a custom chat, and wanted the size of the text box to scale with the length of the text you’re trying to type. Basically, when you run out of space on the first line, the GUI expands and makes space for a second line. My approach was to check the length of the string typed in, but because the letters have slightly more/less space (M is longer than a), it can make the GUI bigger too soon/late. Any suggestions?

Current code:

local numOfLines = 1
while true do
    wait(1)
    local textBox = script.Parent
    if #(tostring(textBox.Text)) >= 50 * numOfLines then
	    numOfLines = numOfLines + 1
	    textBox.Size = UDim2.new(0.9, 0, 0.1 * numOfLines, 0)
    elseif #(tostring(textBox.Text)) == 0 then
	    numOfLines = 1
	    textBox.Size = UDim2.new(0.9, 0, 0.1 * numOfLines, 0)

    end
end

This may be of help here.

It is also better practice to be using TextLabel:GetPropertyChangedSignal("Text") rather than a while loop. The changes take effect instantly and it does not run the code every second.