Problem creating a max size for textbox

this code is meant to cap the size of a textbox, so when it hits the max size limit, it stops auto scaling, and when it goes below, it starts auto scaling again
the problem is that when you hit the max size limit, it oscillates between auto scale and being the correct max size.

i might be completely dumb and this is a simple fix, but idk

Code:

local Input = script.Parent
local InputBox = Input.InputBox
local InputName = Input.InputName

local signal

local function checkSize()
    local nameWidth = InputName.AbsoluteSize.X
    local totalWidth = Input.AbsoluteSize.X

    local maxWidth = totalWidth - nameWidth - 20
    
    if InputBox.AbsoluteSize.X > maxWidth then
        InputBox.Size = UDim2.new(InputBox.Size.X.Scale, maxWidth, InputBox.Size.Y.Scale, InputBox.Size.Y.Offset)
        InputBox.AutomaticSize = Enum.AutomaticSize.None
    elseif InputBox.AbsoluteSize.X < maxWidth then
        InputBox.Size = UDim2.new(InputBox.Size.X.Scale, 41, InputBox.Size.Y.Scale, InputBox.Size.Y.Offset)
        InputBox.AutomaticSize = Enum.AutomaticSize.X
    end
end

signal = InputBox:GetPropertyChangedSignal("AbsoluteSize"):Connect(checkSize)

also heres the element if you want to test yourself
Input.rbxm (9.6 KB)

ended up using a UISizeConstraint, this way we can always leave automatic size on.

local Input = script.Parent
local InputBox = Input.InputBox
local InputName = Input.InputName
local Constraint = InputBox.UISizeConstraint

InputBox.AutomaticSize = Enum.AutomaticSize.X

local function checkSize()
	local nameWidth = InputName.AbsoluteSize.X
	local totalWidth = Input.AbsoluteSize.X

	local maxWidth = totalWidth - nameWidth - 20
	Constraint.MaxSize = Vector2.new(maxWidth, 9e9)
end

checkSize()
InputName:GetPropertyChangedSignal("AbsoluteSize"):Connect(checkSize)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.