generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

TextBox NumValue

Previous Thread :: Next Thread 
ExtremeBuilder15 is not online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
29 Mar 2016 08:26 AM
Use tonumber. If it errors when you don't give it a number, then use pcall.
Report Abuse
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
29 Mar 2016 08:29 AM
Oh, good to know. Thanks.
Report Abuse
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
29 Mar 2016 08:29 AM
No point using pcall. tonumber("swag") will not error, it just returns nil
Report Abuse
Royal_Zey is not online. 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
ExtremeBuilder15 is not online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
29 Mar 2016 08:30 AM
Thanks guys, I'll test it out today!
Report Abuse
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
29 Mar 2016 08:31 AM
Don't use pcall for this <.<
Report Abuse
Royal_Zey is not online. 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
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image