• Attempt to call method (a nil value)
    4 replies, posted
Hey all, Quick question. I like to think of myself as a pretty good coder, however, I always run across 'Attempt to call method (a nil value)' errors and have issues trouble shooting them. I always end up troubleshooting the code and fixing the issue. However, I have yet to find a pattern with these errors. Is there any kind of rule of thumb on what causes these errors? If so, I'm sure it would help me tremendously with troubleshooting. Thanks!
As far as I know, nil means it doesn't exist. Like, when a player spawns, you might set ply.DidHeSpawn=true and later, maybe when he dies, you print ply.DidHeSpawn, if it's nil, then the code to set ply.DidHeSpawn=true didn't get executed. Which means ply.DidHeSpawn is nil.
[url]http://wiki.garrysmod.com/?title=Gmod_Lua_Error_List[/url] also to avoid getting nil values use [lua] if ply.avalue then -- means it isn't nil/false else -- means it is nil or false end [/lua]
[QUOTE=virus7072;28203044]Hey all, Quick question. I like to think of myself as a pretty good coder, however, I always run across 'Attempt to call method (a nil value)' errors and have issues trouble shooting them. I always end up troubleshooting the code and fixing the issue. However, I have yet to find a pattern with these errors. Is there any kind of rule of thumb on what causes these errors? If so, I'm sure it would help me tremendously with troubleshooting. Thanks![/QUOTE] You usually get the 'Attempt to call method (a nil value)'-error when you are trying to use a function in a library that does not exist in the environment it was executed (client/server). Example: Using the vgui, surface, draw libraries on the server.
[QUOTE=Brandan;28203136]As far as I know, nil means it doesn't exist. Like, when a player spawns, you might set ply.DidHeSpawn=true and later, maybe when he dies, you print ply.DidHeSpawn, if it's nil, then the code to set ply.DidHeSpawn=true didn't get executed. Which means ply.DidHeSpawn is nil.[/QUOTE] Right, I was under the same impression, I just didn't know how it applied when calling functions. [QUOTE=Wunce;28203947][url]http://wiki.garrysmod.com/?title=Gmod_Lua_Error_List[/url] also to avoid getting nil values use [lua] if ply.avalue then -- means it isn't nil/false else -- means it is nil or false end [/lua][/QUOTE] Thank you, I was looking for a wiki link, I couldn't find one. Also, thanks for the tip. [QUOTE=_nonSENSE;28207490]You usually get the 'Attempt to call method (a nil value)'-error when you are trying to use a function in a library that does not exist in the environment it was executed (client/server). Example: Using the vgui, surface, draw libraries on the server.[/QUOTE] Thanks for that, I noticed I had a syntax error in the one I was editing, I'm just used to it spitting up the exact error and not an error calling the function. Thanks all for the help!
Sorry, you need to Log In to post a reply to this thread.