Quick Error

[lua]
concommand.Add(“SetArmor”, function(ply, argStr)
ply:SetArmor(tonumber(argStr))
end)
[/lua]

Doesn’t work. I’m so confused. It says that I always have nil(expected number). Thanks for anyone who can help :slight_smile:

Please excuse my incompetence I’m very new.



concommand.Add("SetArmor", function(ply, argStr, args)
	ply:SetArmor(tonumber(args[1]) or 100)
end)


fixed the typo

So the args argument returns a table?

Yep, take a look at the args here.
The 4th arg of that function is a string, not the 2nd. Try this:



concommand.Add("SetArmor", function(ply, cmd, args, argStr)
	ply:SetArmor(tonumber(argStr or 100))
end)


Considering you both use tonumber you should use it right,



ply:SetArgmor(tonumber(args[1]) or 100) 
-- args[1] or argStr depending on which route you take


Just because the string is valid that doesn’t mean it can be converted to a number

So, having the or condition… That means that if they don’t put the argument right ?

Sorry I wrote it on my phone so made a typo (you did too haha)

The or is for if the tonumber function returns nil, so they either entered nothing or a string or something that isn’t a number. Really you should also be checking if the value is negative and isn’t above whatever the max is too

Oh my god, THANK YOU!

This explains so much to me, I see people do this all of the time and I never understood. Thank you!