Wrong type of variable ! How to fix?

Posted by Winddancer on Fri 28 Oct 2022 09:03 PM — 3 posts, 10,418 views.

#0
I want to loot a given number of corpses.
For this I created the following alias:

  <alias
   script="loot"
   match="loot *"
   enabled="y"
   group="exploring"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>%1</send>
  </alias>

The script looks like this:

function loot(x)
	local i=1
        while i <= x
	do
		Send("get all from @i. corpse")
		i=i+1
	end -- do
end -- loot


I am getting the following scripting error:
[string "Script file"]:45: attempt to compare number with string
stack traceback:
[string "Script file"]:45: in function <[string "Script file"]:43>
Line 43 is the function name -> function loot(x)
Line 45 is the while loop -> while i <= x

How do I make sure that the passed variable, x, is interpreted as a number? I tried around with the tonumber() function but didn't get it to work.

Please help
USA Global Moderator #1
Use these instead


<aliases>
  <alias
   match="loot *"
   enabled="y"
   group="exploring"
   regexp="n"
   send_to="12"
   sequence="100"
  >
  <send>loot(%1)</send>
  </alias>
</aliases>



function loot(x)
    local i=1
    while i <= x do
        Send("get all from " .. i .. ".corpse")
        i=i+1
    end
end
#2
Thanks, this works as expected. :)