Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Weird error

Weird error

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Shadoweave   (42 posts)  Bio
Date Mon 24 Sep 2007 06:22 AM (UTC)

Amended on Mon 24 Sep 2007 06:58 AM (UTC) by Nick Gammon

Message
When I run this script:

function change_settings (sName, sLine, wildcards)
settings[wildcards[1]] = tonumber(wildcards[2])
end -- function


the following script:

function prompt (sName, sLine, wildcards)
i = 1
  	repeat
    	if  tonumber (GetVariable ("c_"..healpriority[i])) < 
            tonumber( GetVariable( "max"..healpriority[i] ) ) * (settings[healpriority[i]]) then
			elixbal_aff[healpriority[i]] = hme_cure[healpriority[i]]
			i = 3
		end -- if
	i = i + 1
	until i > 3
elixbal_check ()
end -- function

keeps giving me this error:

[string "Script file"]:1644: attempt to perform arithmetic on field '?' (a nil value)
stack traceback:
	[string "Script file"]:1644: in function <[string "Script file"]:1622>

The error is on the "if" line, more specifically where I try to multiply. The weird thing is, when I reload the script file it works without any problems. I am baffled, any suggestions?
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 24 Sep 2007 06:56 AM (UTC)

Amended on Mon 24 Sep 2007 06:59 AM (UTC) by Nick Gammon

Message
tonumber returns nil if the string cannot be converted to a number, see:

http://www.gammon.com.au/scripts/doc.php?lua=tonumber

Since running change_settings causes the problem, I would look there. You could test that you have a valid number like this:


function change_settings (sName, sLine, wildcards)
  settings[wildcards[1]] = assert (tonumber(wildcards[2]), 
                           "wildcard '" .. tostring (wildcards [2]) .. "' is not a number")
end -- function


The "assert" function checks that its first argument is true (that is, not nil and not false) and if so, returns that as its result (thus you can use it in an assignment).

However if the assertion fails, you get an error message. The way I have worded it, you will see what wildcard 2 is, so you can see why it is becoming nil.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Shadoweave   (42 posts)  Bio
Date Reply #2 on Mon 24 Sep 2007 08:01 AM (UTC)

Amended on Mon 24 Sep 2007 11:39 AM (UTC) by Shadoweave

Message
You were right, that was the problem. The wildcards[2] variable was 1/4, and it wasn't interpreting it as a number, or as a string. If I use .5, it recognizes it as a number, so there is no need to use the "tonumber" function.



Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,657 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.