Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a TextBox numbers-only?

Asked by 7 years ago

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

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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!

Ad

Answer this question