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 » Scripting Helpers
Home Search
 

Re: tonumber function.

Previous Thread :: Next Thread 
gbs is not online. gbs
Joined: 10 Apr 2008
Total Posts: 16823
22 Oct 2012 05:45 PM
local health = tonumber(string.sub(msg,slash+1))

If the string is, let's say, 100000, then the script works fine. It does not seem to work if the string is math.huge (will say health = nil, I believe). Is there any way around this?
Report Abuse
gbs is not online. gbs
Joined: 10 Apr 2008
Total Posts: 16823
22 Oct 2012 05:51 PM
local health = tonumber(1000)
vs.
local health = tonumber(math.huge)
Easier format to read.
Report Abuse
Woodstauk4 is not online. Woodstauk4
Joined: 27 Dec 2010
Total Posts: 3061
22 Oct 2012 05:53 PM
math.huge -> 1.#INF

Infinity is not a real number, so it doesn't return a normal value when utilized. I don't understand the problem with the information given, though.
Report Abuse
gbs is not online. gbs
Joined: 10 Apr 2008
Total Posts: 16823
22 Oct 2012 05:56 PM
I'm trying to set a player's maxhealth to math.huge if they type

health/soandso/math.huge

Setting math.huge to a string, then tonumber, then maxhealth to the number.
Report Abuse
Woodstauk4 is not online. Woodstauk4
Joined: 27 Dec 2010
Total Posts: 3061
22 Oct 2012 05:56 PM
What is 'slash'?
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
22 Oct 2012 05:59 PM
math.huge isn't actually a number, it's a variable.... math.huge returns the HUGE_VAL number which is the equivalent of 2^1024 because that is the highest number that a double can hold. If you wanted you could use 0x58A213CC7A4FFDA54CE688E7F0BC because that is the hexidecimal version of 2^1024. Or you could change your code up to be something like

local health = loadstring("return "..msg:sub(slash+1))();

However this could lead to a lot of exploits.
Report Abuse
nairod7 is not online. nairod7
Joined: 26 Mar 2010
Total Posts: 869
22 Oct 2012 06:01 PM
local health = (string.sub(msg,slash+1) == "math.huge" and math.huge) or tonumber(string.sub(msg,slash+1))
Report Abuse
L2000 is not online. L2000
Joined: 03 Apr 2008
Total Posts: 77448
22 Oct 2012 06:03 PM
tonumber("math.huge")

It doesn't count that as a number (At least, last time I checked it didn't), so it returns nil when trying to get a number version of it. If you want it like that, you need to do this:

if msg:sub(slash + 1):lower() == "math.huge" then -- The string
health = math.huge; -- The number
end

Which could also be (More efficient):

local special = {
["math.huge"] = math.huge
}

And then have this:
local health = tonumber(msg:sub(slash + 1)) or special[msg:sub(slash + 1)]

Which would check if it's a number, and return that if it is; otherwise, it will try to find it in the table, returning the value from the table it is (Or nil if it's not there).
Report Abuse
gbs is not online. gbs
Joined: 10 Apr 2008
Total Posts: 16823
22 Oct 2012 06:05 PM
Slash is a variable that holds the place of the second / chatted by the player: not important in this discussion.

Sorry to say, but I have no idea what loadstring does.

I'll have to try that, nairod.
Report Abuse
Woodstauk4 is not online. Woodstauk4
Joined: 27 Dec 2010
Total Posts: 3061
22 Oct 2012 06:07 PM
L2000, was that last line of script you posted utilizing a ternary operator? You can use them like that?
Report Abuse
nairod7 is not online. nairod7
Joined: 26 Mar 2010
Total Posts: 869
22 Oct 2012 06:08 PM
This would be better:

local health = tonumber(string.sub(msg,slash+1)) or math.huge
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
22 Oct 2012 06:10 PM
@nairod

What if they put 14! or hello?
Report Abuse
nairod7 is not online. nairod7
Joined: 26 Mar 2010
Total Posts: 869
22 Oct 2012 06:19 PM
Indeed :s, so my first expression is better, right?

local health = (string.sub(msg,slash+1) == "math.huge" and math.huge) or tonumber(string.sub(msg,slash+1))
Report Abuse
gbs is not online. gbs
Joined: 10 Apr 2008
Total Posts: 16823
22 Oct 2012 06:47 PM
Nairod's version seems to do the trick (along with a :lower() just because).

That's what I love about scripting, so many ways to accomplish a task.

Thanks for the help all :)
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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