attempt to index global 'ply' (a nil value)

Error:




[ERROR] lua/autorun/eventprop.lua:14: attempt to index global 'ply' (a nil value)
  1. unknown - lua/autorun/eventprop.lua:14
   2. unknown - lua/includes/modules/concommand.lua:69


Code:



	if ( SERVER ) then
	
		hook.Add( "PlayerSay", "playersayminigame", function( _p, _text, public )
	
			if ( _text == "/rawrgoesdinosaurs" ) then
				_p:ConCommand( "rawrgoesdinosaurs" );
				return "";
                        end
		end );
	
	else
	
		concommand.Add( "rawrgoesdinosaurs", function() 
			ply:SendLua([[RunConsoleCommand("amg_start", "prop", "90")]]);
		end );
		
	end
	


Really need help with this. It’s driving me crazy.

Concommands use 3 arguments. Player CommandTyped, Arguments.


concommand.Add( "rawrgoesdinosaurs", function( _p, _cmd, _args ) 
	_p:SendLua([[RunConsoleCommand("amg_start", "prop", "90")]]);
end );

Just like the first one… _p is ply

Now getting this error:




[ERROR] lua/autorun/eventprop.lua:14: attempt to call method 'SendLua' (a nil value)
  1. unknown - lua/autorun/eventprop.lua:14
   2. unknown - lua/includes/modules/concommand.lua:69


SendLua is a server side function, you do an if SERVER then check, meaning you’re running SendLua from the client side interface.

Make sure SendLua is called on the server, not the client.

Don’t use sendlua for anything in production code… at all.

As for the first error:
“attempt to index ‘variable’ (a nil value)”

This means that a variable was attempted to be indexed, but it’s nil and you can’t index nil.
Indexing means trying to call a method or get a property from the variable (table[“key”], etc)

So, how would I call it server side?

[editline]2nd September 2014[/editline]



	if ( SERVER ) then
	
		hook.Add( "PlayerSay", "playersayminigame", function( _p, _text, public )
	
			if ( _text == "/rawrgoesdinosaurs" ) then
				_p:ConCommand( "rawrgoesdinosaurs" );
				return "";
                        end
		end );
	
	else
	
		concommand.Add( "rawrgoesdinosaurs", function( _p, _cmd, _args ) 
				_p:ConCommand("amg_start", "prop", "90");
		end );
		
	end
	


Is this better?

If you are trying to make an addon, try learning Lua first. It’ll help you greatly, and honestly it’s a million times better than passing time being spoonfed code and not understanding the hell you are trying to do.

There are a few tutorials on Facepunch, you can try the official manual on lua.org, and use both old and new Garry’s Mod wikis.

Anyway, this code is valid, but I’m not sure what you are trying to do in this console command you are creating. You should just use RunConsoleCommand then pass the command and its arguments one by one. Or even, _p:ConCommand(“amg_start prop 90”) in that hook.

(ex. RunConsoleCommand(“gm_spawn”, “some_model_path”), that’d make spawn the specified model on the Sandbox gamemode)