How do I add a max number limit for text boxes?

Basically I have an age changer GUI but I want it so if you go past a certain age it won’t allow you to have that age. My code isn’t working for some reason, the error is: “attempt to compare string and number”

My code is :

local remote = game:GetService("ReplicatedStorage").Events.Age
local TextBox = script.Parent.CharacterFrame.TextBox




script.Parent.CharacterFrame.Submit.MouseButton1Down:Connect(function()
	    if TextBox.Text <40 then
    	script.Parent.CharacterFrame.Visible = false
    	script.Parent.MainFrame.Visible = true
    		remote:FireServer()

    	elseif TextBox.Text >40 then
    	print("not acceptable")
    	end


    	

    end)

Textbox.Text is a “string”. And a number is a number. You can not compare a string and a number but if your textbox.Text is only a number then, you can use the tonumber function to convert it.

local number = tonumber(textbox.Text)
3 Likes

You might also want to do some text masking here and enforce numerical input only just so it makes things easier, because tonumber will return nil if a non-number is present in the string which will of course make your code fail. If you can “guarantee” (exploiters can bypass, you have to account for this from the server) that the input will only be text boxes, it’ll be much safer to work with tonumber.

TextBox:GetPropertyChangedSignal("Text"):Connect(function ()
    TextBox.Text = string.gsub(TextBox.Text, "%D+", "")
end)

I have a gui thats uses buttons to add +1 and -1 values
Heres a pic:

the textbox text isnt editable

Oh, are you using a TextLabel? I assumed it was a TextBox because the variable name and the instance name are both “TextBox” and the thread’s title was also a bit too short to understand.

Buttons work as well, though it may not be the most appropriate in terms of UX since players may find it annoying to have to click to their desired number instead of just being able to quickly type it out. Just my two cents though, not really that relevant.

I changed the title thinking you were looking for limiting the number of characters, not a number that had a different method of input. My bad! Changed title to reflect accordingly.

2 Likes

No problem and yea it is a textbox. I should change it to a textlabel though. Thanks for your input!