How do I check if something is a number?...
✓ ANSWER ACCEPTED
How do I check if something is a number?
1 answer
✓ Accepted answer
You can use the type function to return the value of something.
local function checknumber(num)
if type(num) == "number" then
return true
else
return false
end
end
print(checknumber(3)) --prints true
print(checknumber("hello!")) --prints false
4 comments
0
[string "stdin"]:2: attempt to index local 'num' (a number value)
0
Your code errors because you are attempting to use a method on a number type.
0
Sorry, I haven't used it in awhile. I forgot it wasn't a method. Edited.
1
No problem, thank you for your answer!
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Use the type() function. It will compare the argument to a string value to provide a result. if type(Value) == 'number' then return true end --[[OR you can do]] if tonumber(Value) ~= nil then return true end
if type(variable) == 'number' end 'number' is a string value?
http://www.lua.org/pil/2.html. Might help