megaman54 Posted January 22, 2012 Posted January 22, 2012 How to check if argument typed by a player in a command is a number?
FatalTerror Posted January 22, 2012 Posted January 22, 2012 if not tonumber(x) then -- error: x is not a number Source: http://www.lua.org/pil/8.3.html
Kenix Posted January 22, 2012 Posted January 22, 2012 You mean this? print( type( 1 ) ) -- print number Example: function main( arg ) if type( arg ) == 'number' then return true end return false end print( main( 1 ) ) --> true
megaman54 Posted January 22, 2012 Author Posted January 22, 2012 Thanks for both of you! I will try both of those examples
Kenix Posted January 22, 2012 Posted January 22, 2012 No problem Test this in lua demo http://www.lua.org/cgi-bin/demo
DiSaMe Posted January 22, 2012 Posted January 22, 2012 You mean this? print( type( 1 ) ) -- print number Example: function main( arg ) if type( arg ) == 'number' then return true end return false end print( main( 1 ) ) --> true Wrong. print(type("1")) will return false. Command arguments are always strings, so no matter what player writes, it will give the same results.
Kenix Posted January 22, 2012 Posted January 22, 2012 print(type("1")) will return false Nope,wrong. It return string ,i test it in lua demo. Command arguments are always strings, so no matter what player writes, it will give the same results. Yeah,but if player not use arguments in command ( just /some ) we can check it with type function or with condition if ... then ( this is same ). Example addCommandHandler( "some", function( soruce,_,a ) if type( a ) == 'string' then outputChatBox( "string" ) else outputChatBox( "WRONG:Use /some and arguments" ) end end ) P.S If he need test this in mta: print( type( "1" ) ) Just outputChatBox( type( "1" ) )
DiSaMe Posted January 22, 2012 Posted January 22, 2012 Oh, i mean, print(main("1")) will print false if we use your "main" function. megaman54 didn't ask for a way to check if an argument exists (in addition, checking the type is useless, just compare the variable to nil). He asked for a way to determine whether the player typed in a number. FatalTerror showed how to do this correctly. Your example is wrong because it doesn't check whether the string contains any numbers.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now