Why doesn't this work?

Posted by Curious2 on Mon 02 Feb 2009 09:07 PM — 4 posts, 15,711 views.

#0
It keeps saying I am trying to compare a string to number...

[string "Plugin"]:752: attempt to compare number with string

751 : local halfmana = stats.maxmana / 2
752*: if stats.mana > halfmana == false or

The values in the table are numbers not strings?

This should store them as numbers right?

_, _, stats.health, stats.maxhealth, stats.mana, stats.maxmana, stats.endur, stats.maxendur, stats.will, stats.maxwill, stats.lvl, stats.maxlvl = string.find (text, "H:(%d+)/(%d+) M:(%d+)/(%d+) E:(%d+)/(%d+) W:(%d+)/(%d+) NL:(%d+)/(%d+)")

Also are you able to have two stats tables in differen plugins?
#1
Well...it is saving the values as strings and not numbers. Even if I use tonumber it still says the type is string...

How can I force them to save as numbers?

For example, I made a test strigger and in a script looped through and printed the type of each value and it showed

string
number
number
number
string
string
number
number
number
string
string
string
string
string
string
string

The strings are what is being saved by the string.find.
Australia Forum Administrator #2
string.find and string.match always return strings, regardless of the regular expression.

Just because you search for %d+ doesn't make string.find do a conversion into numbers. After all, something that matches %d+ might be:


459787947495673936760979573462558790262414


That wouldn't be storable in a number (at least, not without throwing away precision).

One approach is to save as temporary variables and then convert, eg.


local health, maxhealth, mana, maxmana, endur, maxendur, will, maxwill, lvl, maxlvl = string.match (text, "H:(%d+)/(%d+) M:(%d+)/(%d+) E:(%d+)/(%d+) W:(%d+)/(%d+) NL:(%d+)/(%d+)")

stats.health = tonumber (health)
stats.maxhealth = tonumber (maxhealth)

-- and so on


Note that string.match is similar to string.find, but doesn't return the the start and end positions. So if you are doing this:


_, _, a = string.find (str, "(%a+)")


You may as well do this instead, which is shorter and neater:


a = string.match (str, "(%a+)")



Another approach is to convert when required, eg.


local halfmana = stats.maxmana / 2
if tonumber (stats.mana) > halfmana == false or


Note that Lua realizes to convert something like "stats.maxmana / 2" into a number for you, so it didn't fail on that line.

Also I don't particularly like this, it looks a bit clumsy:


if stats.mana > halfmana == false then


Comparing "== true" or "== false" is rarely necessary. This looks better:


if not stats.mana > halfmana  then


Or why not just put it the right way around?


if stats.mana <= halfmana  then


Code like:


if stats.mana > halfmana == false then


... just makes me stop and wonder what is being tested for really. Whereas:


if stats.mana <= halfmana  then


... is much more obviously testing if you have less than half your mana.
#3
Haha well that code was from a plugin I downloaded from here. As far as the comparison, I am not sure why I did that because all of the others I put exactly like you have. Probably lack of sleep I guess. I feel retarded now. Forgive me.