Although creative, that method will just cause an error. Even if you type in a number, it's still a string (yes, "5"
is a string), and IntValues error if you try to give them strings.
A common method is to use the tonumber
function. This function attempts to convert a string into a number, and then return that number. If the string isn't an actual number, then it just returns nil.
2 | print ( tonumber ( "Hello world!" )) |
It is usually sufficient to just check when you get the input if it's a number or not;
1 | if tonumber (textBox.Text) then |
Although you could just directly not allow any non-number input;
1 | if not tonumber (textBox.Text) then |
On a side note, don't use while
loops like you're doing. It's much better to use events, such as Changed, for efficiency purposes.