Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a TextBox numbers-only?

I'm making a textbox that allows players to edit an IntValue, but I don't want letters to be able to go in the TextBox. How exactly do I make it only digits?

1 answer

✓ Accepted answer

Hi!

This is solved with string manipulation. We can use gsub to delete non-digits every time the text is changed.

How gsub works: Gsub consists of 3 separations; the string, the pattern, and the replacement text.

print( string.gsub("hello world", "hello", "goodbye") ) --Prints 'goodbye world'

However, we can use Character Classes to tell it to keep that part of the string. "%D" refers to digit numbers 0-9 that will be kept.

print(string.gsub("9893Hi There9392","%D","")) --Prints '98939392'

Code: Now, we can use that string manipulation every time the TextBox is changed.

TextBox.Changed:connect(function()
	TextBox.Text=string.gsub(TextBox.Text,"%D","")
end)

And that's all!

This discussion is preserved as it was. New answers, comments, and votes are closed.