|
| 29 Mar 2016 08:18 AM |
| Is there a way to check if what the user has typed into a textbox is a numbervalue and can be converted into one? I've been having trouble with this for a few days. |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2016 08:26 AM |
| Use tonumber. If it errors when you don't give it a number, then use pcall. |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2016 08:28 AM |
Using tonumber on a value that can not be converted to a number will NOT error. Just do
local number = tonumber(textBox.Text) if number then print("it's a number!") end |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2016 08:28 AM |
For example
local Success, Number = pcall(tonumber, InputString) if Success then -- It's a number! Yay! print(Number) end |
|
|
| Report Abuse |
|
|
| |
|
|
| 29 Mar 2016 08:29 AM |
| No point using pcall. tonumber("swag") will not error, it just returns nil |
|
|
| Report Abuse |
|
|
Royal_Zey
|
  |
| Joined: 25 Mar 2016 |
| Total Posts: 233 |
|
|
| 29 Mar 2016 08:30 AM |
Taken from wiki.roblox.com
tonumber[edit] tonumber (e [, base])
Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil.
So tonumber will return nil if it isn't convertible.
And like Jarod said, pcall will help continue the thread regardless of errors. |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2016 08:30 AM |
| Thanks guys, I'll test it out today! |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2016 08:31 AM |
| Don't use pcall for this <.< |
|
|
| Report Abuse |
|
|
Royal_Zey
|
  |
| Joined: 25 Mar 2016 |
| Total Posts: 233 |
|
|
| 29 Mar 2016 08:31 AM |
@dire
It will if he tries for instance using the so imaginary number (nil) in some functions that would error due to the value being different from what they want. |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2016 08:35 AM |
Just use an if statement :I Like this...
local number = tonumber(textBox.Text) if number then end |
|
|
| Report Abuse |
|
|