• "attempt to index local 'ply' (a nil value)"
    12 replies, posted
I'm getting this damn error: attempt to index local 'ply' (a nil value) [lua] function GetPlayer() if !SERVER then if ValidEntity(LocalPlayer()) then return LocalPlayer( ) end end end function Blah( ply ) local ply = ply local ply = GetPlayer() if CLIENT then return end if !ply:isValid() then return end --Getting error here end concommand.Add( "GOCOMMAND", Blah( GetPlayer() ) ) [/lua] I placed the file in the lua folder and I'm opening it with lua_openscript.
Are you trying to use this serverside or clientside? I'm asking because it doesn't make any sense. What are you even trying to do here?
I'm doing both, I want it to run on both server and client. I don't want to make the annoying includes and split up my code to 3 different files.
what are you trying to do?
I basically want to do a bunch of serversided stuff like creating effects or whatever. However I need the vector of the player initializing the command. However I can't even assign the client's entity to a variable. I found another way but I would still like to find out why the LocalPlayer doesn't retrieve the player sending the command.
[lua] local ply = LocalPlayer() function Blah() if not ValidEntity(ply) then return end; if ply:IsAdmin() then ply:SetHealth(1000) end end [/lua]
lua_openscript only runs the script on the server unless I'm mistaken. If you want to get the player that ran the command on either server or client you need only use the ply argument in the command function. What you wrote makes no sense to me, I have no idea how you expected it to work. If you want to know why you're getting that error, the code tries to set ply to the return value of GetPlayer(). But it's running on the server, so it skips all the code in it because of the if !SERVER then. And doesn't return anything. So now ply is nil. It gets to the error-producing line and tries to see if ply is a valid entity. But hold on ply doesn't exist, it's nil, we have to throw an error.
But I added [lua] if !SERVER then [/lua] Shouldn't that make it clientsided and then store the value?
No, the script just checks if it's running on the server. And if it is then it skips the stuff between that line and the end that goes with it. It doesn't switch the code to running on the client. As I've said before, you said you wanted to "assign the client's entity to a variable", but you've already got it as a variable. The first argument of the function. And because you're using lua_openscript, as I've also said before, the script is [B]ONLY[/B] running on the server. As a side-note, don't put the brackets when you use concommand.Add. Just put the name of the function on its own.
What the hell guys?! It's easy! concommand.Add("GOCOMMAND", Blah(GetPlayer()) This is the problem it's evaluating GetPlayer() for a string it can use. Here's what the whole thing should look like (Stop me if I'm making a fool of myself) : [lua]function Blah(ply) if CLIENT then --client stuff end if SERVER then --server stuff end concommand.Add("GOCOMMAND", Blah)[/lua] The player running the command is always the first argument in the function it is linked to. And a simple instance check is probably all you need. But even that is pointless seeing as it if it is registered on both the client and the server it will be called on both either way (unless that's the point). [url=http://wiki.garrysmod.com/?title=concommand.Add][i]concommand.Add[/i][img]http://wiki.garrysmod.com/favicon.ico[/img][/url] [editline]01:06AM[/editline] Oh MegaJohnny's already explained most of that. <3
Quiet you.
Sorry, you need to Log In to post a reply to this thread.