Number Only Text Box

How can I make a Number Only Text Box, so if they try to put in letters or characters, it doesn’t go, but if they do a number 0-9, it can be typed in the textbox.

3 Likes

You can do this by hooking the text box to a .Changed event, and every time a character is typed, if tonumber() on the text fails delete the last character added.

5 Likes

you can use string.gmatch with the %d pattern to filter out all numbers in a string
for example

string = "1Hogs2Moss" 
for numbers in string.gmatch(string,"%d") do
 print(numbers) 
end

returns “1 2”

4 Likes

Use :gsub to replace non-digits, and use :GetPropertyChangedSignal method to detect changes in the TextBox. Sometrhing like this

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
    TextBox.Text = TextBox.Text:gsub("%D", '');
end);
5 Likes