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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
| |
|
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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
| Joined: 13 Nov 2009 |
| Total Posts: 1911 |
|
|
| 22 Oct 2012 06:10 PM |
@nairod
What if they put 14! or hello? |
|
|
| Report Abuse |
|
|
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
|
  |
| 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 |
|
|