How can I find out if a value is a number? Because while testing my upcoming Evolve money project, a friend was able to set his money to "leg" which of course is a string/word. I can easily prevent this but I need to know how to find out if something is a number or a string, e.g tell the difference between "leg" and 5000.
Tl;dr: How to find difference between string and number in lua.
Use type(variable) == "number"
[QUOTE=Chessnut;32818499]Use type(variable) == "number"[/QUOTE]
That will just print everything as string because evolve (I don't use it myself but all concommands behave this way so I assume that it does that too) use strings for args, so the number will be a string. you could use Lua's regex to check if its a number.
Probably a bad example, but it works:
[lua]string.find(stringasnumber, "^%d") -- returns nil if something else but numbers are in there, returns a number if only numbers are in the string[/lua]
[editline]16th October 2011[/editline]
Wait, that doesn't actually work, but I'm no good with regex..
[editline]16th October 2011[/editline]
Okay this should work:
[lua]string.find(stringasnumber, "[^%d]") -- returns true if something else but a number is in there, nil if there are only numbers in the string[/lua]
Would
[lua]if args:type(variable) == "number"[/lua]
be correct?
type() is a function that returns the type of the argument.
Hm, here is a code snippet from the project
[lua]
function PLUGIN:Call( ply, args )
if ( ply:EV_HasPrivilege( "Set Money" ) ) then
if ( #args > 0 ) then
ply:SetProperty( "Money", tonumber(args[1]) )
evolve:Notify( evolve.colors.white, "You have set your balance to: ", evolve.colors.red, args[1] )
evolve:CommitProperties()
end
else
evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
end
end
[/lua]
Use the string.find code I gave you, that should work for it.
[lua]function PLUGIN:Call( ply, args )
if ( ply:EV_HasPrivilege( "Set Money" ) ) then
if ( #args > 0 ) then
if not string.find(args[1], "[^%d]") then
ply:SetProperty( "Money", tonumber(args[1]) )
evolve:Notify( evolve.colors.white, "You have set your balance to: ", evolve.colors.red, args[1] )
evolve:CommitProperties()
end
end
else
evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
end
end[/lua]
Testing it now..
if its a string and you want to check if its a number you can do,
[LUA]
if(tonumber(args[1]))then
end
[/LUA]
or
[LUA]
if(!tonumber(args[1]))then
end
[/LUA]
Alright leiftiger your code worked, How would I make it say, display an evolve.Notify(evolve.colors.white, "You cannot set your balance to a word!") in the code snippet I gave you?
Edit:
I'm thinking along the lines of:
[lua]
function PLUGIN:Call( ply, args )
if ( ply:EV_HasPrivilege( "Set Money" ) ) then
if ( #args > 0 ) then
if string.find(args[1], "[^%d]") then
evolve.Notify(evolve.colors.white, "You cannot set your balance to a word!") end
if not string.find(args[1], "[^%d]") then
ply:SetProperty( "Money", tonumber(args[1]) )
evolve:Notify( evolve.colors.white, "You have set your balance to: ", evolve.colors.red, args[1] )
evolve:CommitProperties()
end
end
else
evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
end
end
[/lua]
You could add else to it:
[lua]
function PLUGIN:Call( ply, args )
if ( ply:EV_HasPrivilege( "Set Money" ) ) then
if ( #args > 0 ) then
if not string.find(args[1], "[^%d]") then
ply:SetProperty( "Money", tonumber(args[1]) )
evolve:Notify( evolve.colors.white, "You have set your balance to: ", evolve.colors.red, args[1] )
evolve:CommitProperties()
else
evolve.Notify(evolve.colors.white, "You cannot set your balance to a word!") -- But won't this show to all players? (I don't know anything about evolve)
end
end
else
evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
end
end[/lua]
However I would probably recommend using -TB-'s code, it seems a bit better to use that instead of regex.
Ah, and yes it will show to all players, it's for debugging at the moment
Edit: Testing -TB-'s code instead
[editline]16th October 2011[/editline]
His code doesn't work.. reverting to yours..
Edit: It's all up and running with your code!
Thanks everyone!
Sorry, you need to Log In to post a reply to this thread.