Can someone help me optimize my textbox?

textBoxTable.TextBoxInstance:GetPropertyChangedSignal("Text"):Connect(function()
		
		local inputStr = tostring(textBoxTable.TextBoxInstance.Text):gsub(" ", "")

		if inputStr == "-" then
			textBoxTable.TextBoxInstance.Text = "-"
		elseif inputStr == "." then
			textBoxTable.TextBoxInstance.Text = "."
		end

		local hasTrailingDot = inputStr:sub(-1) == "." -- checks if the last character is a period (.).

		print(hasTrailingDot)

		inputStr = inputStr:match("^[%d%.%-]*")

		local dotCount = select(2, inputStr:gsub("%.", ""))
		if dotCount > 1 then
			inputStr = inputStr:match("^[%d%.]+")
		end

		inputStr = inputStr:gsub("^(%-?)0+(%d)", "%1%2")
		inputStr = inputStr:match("^%-?%d*%.?%d*")

		if inputStr:find("%.") then
			local integerPart, decimalPart = inputStr:match("^(%-?%d*)%.(%d*)")
			if decimalPart and #decimalPart > 3 then
				decimalPart = decimalPart:sub(1, 3)
			end
			inputStr = integerPart .. (decimalPart and "." .. decimalPart or "")
		end

		if hasTrailingDot and not inputStr:find("%.") then
			inputStr = inputStr .. "."
		end

		textBoxTable.TextBoxInstance.Text = inputStr
	end)
1 Like

I feel like

inputStr = inputStr:match("^[%d%.%-]*")

local dotCount = select(2, inputStr:gsub("%.", ""))
if dotCount > 1 then
	inputStr = inputStr:match("^[%d%.]+")
end

might be redundant

Also, is it possible to only have 4 digits before the dot? So the largest possible number would be 9999.999, and the smallest would be -9999.999.