What’s wrong with this lua code, my argument is never converted to a number or recognized as a number no matter what I type?
I tried “distance = tonumber(arg[0]) or 0” as well.
--Args
local args = {...}
--Variables
local distance = 0
if #args > 0 and type(args[0])=="string" then args[0] = tonumber(args[0]) end
if #args > 0 and type(args[0])=="number" then distance = args[0] end
print("Distance: "..distance)
Lua uses 1-based indices for its arrays.
args[0]isnil, and therefore has the type"nil".By the way, this condition is entirely unnecessary.
tonumberwill check to see if its argument is a number and simply return it if needed. It will returnnilif the argument cannot be converted to a number. So just use:You don’t even need to check the length of
args; if no arguments were provided, it will benil, andtonumberwill returnnil. Thus, just check to see ifdistanceisnil.