Why do I have this error?

So I have this error:


[ERROR] gamemodes/sandbox/gamemode/cl_init.lua:444: attempt to index global 'Pla
yer' (a function value)
  1. DoClick - gamemodes/sandbox/gamemode/cl_init.lua:444
   2. unknown - lua/vgui/dlabel.lua:206

And this error:


[ERROR] gamemodes/sandbox/gamemode/cl_init.lua:444: attempt to call method 'Give
' (a nil value)
  1. DoClick - gamemodes/sandbox/gamemode/cl_init.lua:444
   2. unknown - lua/vgui/dlabel.lua:206

I tried this:


ply:Give("weapon_crowbar")

But that didn’t seem to work!

Here is my code:


local button2 = vgui.Create( 'DButton' )
			button2:SetParent( gra )
			button2:SetSize( 130, 25 )
			button2:SetPos( 159, 200 )
			button2:SetText( '' )
			button2.DoClick = function()
				Player:Give("weapon_crowbar")
			end

  • Thanks!

Player is not defined in your code, so Lua automatically assumes you want the Global function Player().

What you are doing in your DoClick is:

Function:Function( “” )

How would I go about defining the player?

Also, ENT:Give is server-side only. You’re going to need to network something to the server and let the server give you the weapon: http://wiki.garrysmod.com/page/Player/Give

[editline]1st November 2014[/editline]

On CLIENT the player is always accessible via LocalPlayer( ), however there are instances when the player is first joining where LocalPlayer( ) will be NULL / nil, so you’d need to test for that. Example: https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html

player:Give() is server side, not client side :v:

Put this code on top


local Player = FindMetaTable( "Player" )

Accessing the meta-table is something entirely different than what is required here unless you’re suggesting he write a clientside Give function which networks the data to the server ( not recommended to ever trust user-input because that would give the client access to any entity that can be given to a player ).

Here’s an example of networking data to the server and getting a response ( in terms of identifying a player ): https://dl.dropboxusercontent.com/u/26074909/tutoring/networking/networking_player_identity_from_client_to_server_and_back.lua.html

You won’t need all of that, you’d need a simple 1 way message to the server ( I’d recommend keeping a SHARED table of codes that can be sent to the server [ so we’re not trusting user input ] so the server can verify that the code exists and what it is supposed to do )…

Ok couldn’t I just set up a command for this? Something like:


concommand.Add( "spawnpaint",function( ply )
		
		Player:Give( 'weapon_spraymhs' )
	
	end )
	

[lua]
// Server side
util.AddNetworkString(“spawn_paint”)
net.Receive(“spawn_paint”, function(len, client)
if !client:HasWeapon(“weapon_spraymhs”) then
client:Give(“weapon_spraymhs”)
end
end)
// Client side
function SpawnPaint()
net.Start(“spawn_paint”)
net.SendToServer()
end[/lua]
Now use SpawnPaint to give player the paint thing clientside

Well, when I clicked the button it didn’t give me my paint.

What’s the code?

Actually, I do have something in line with what you need: https://dl.dropboxusercontent.com/u/26074909/tutoring/_zipped_code/acecool_npc_jointeam_example.rar


// Client side of sh_npc_jointeam.lua
		_bjoin.DoClick = function( )
			net.Start( "npc_jointeam_cl_to_server_join_team" );
			net.SendToServer( );
		end

// Server side of sh_npc_jointeam.lua

	util.AddNetworkString( "npc_jointeam_cl_to_server_join_team" );

	//
	// This is the serverside receiver for what the client sends to the server...
	//
	net.Receive( "npc_jointeam_cl_to_server_join_team", function( _len, _p )
		// Since we know the client had to press the join button... Just join..
		_p:KillSilent( );
		_p:SetTeam( 2 );
		_p:Spawn( );
		_p:PrintMessage( HUD_PRINTTALK, "You've been set to Team 2; hopefully someone remembered to create it!" );
	end );


That covers the DoClick networking to server, the server setup for net message, the net receive on the server and actions performed on the server. To expand it, set up a shared table such as:


NETWORKING_WEAPON = 0;
NETWORKING_TEAM = 1;

WEAPON_CROWBAR = 0;
WEAPON_PISTOL = 1;

JOINTEAM_SPECTATOR = 0;
JOINTEAM_ALIVE = 1;

NETWORKING_ACTIONS = {
	[ NETWORKING_WEAPON ] = {
		[ WEAPON_CROWBAR ] = function( _p )
			_p:Give( "weapon_crowbar" );
		end;
		[ WEAPON_PISTOL ] = function( _p )
			_p:Give( "weapon_pistol" );
		end;
	};
	[ NETWORKING_TEAM ] = {
		[ JOINTEAM_SPECTATOR ] = function( _p )
			_p:SetTeam( TEAM_SPECTATOR );
			_p:KillSilent( );
		end;
		[ JOINTEAM_ALIVE ] = function( _p )
			_p:SetTeam( 1 );
			_p:Spawn( );
		end;
	};
};


Network 2 ints, here’s how to count bits: https://dl.dropboxusercontent.com/u/26074909/tutoring/__documentation/bit_counting.lua

So WriteInt( NETWORKING_WEAPON, 3 ); WriteInt( WEAPON_CROWBAR, 3 ); – extra bit because a signed integer ( means that the - or + is sent with it ) requires a bit.

The server then looks up the table to see if NETWORKING_WEAPON exists, then if WEAPON_CROWBAR exists, then passes the player in… This is just a rushed simple version of how you can set something up ( it is still somewhat trusting user input because you can select any weapon. You’d add checks in the function to ensure they can get it, etc… )


local button2 = vgui.Create( 'DButton' )
			button2:SetParent( gra )
			button2:SetSize( 130, 25 )
			button2:SetPos( 159, 200 )
			button2:SetText( '' )
			button2.DoClick = function()
				function SpawnPaint()
					net.Start("spawn_paint")
					net.SendToServer()
				end

You could ( if it is serverside ) but using console commands as a means to network is typically frowned upon…

That command didn’t work. And everything you are showing me is so damn confusing. I didn’t think pressing a button to give a weapon would be this confusing!

[lua]
button2.DoClick = function()
function SpawnPaint()
net.Start(“spawn_paint”)
net.SendToServer()
end
[/lua]
wtf is this. can you even code? remove everything but “SpawnPaint()”
tl;dr make the function outside of the code, then run it inside the code. whenever they press the button the function gets created, not ran.

Well now, I’m getting an error after doing what you said here is the error and code:


[ERROR] gamemodes/sandbox/gamemode/cl_init.lua:468: unexpected symbol near ')'
  1. unknown - gamemodes/sandbox/gamemode/cl_init.lua:0



local button2 = vgui.Create( 'DButton' )
			button2:SetParent( gra )
			button2:SetSize( 130, 25 )
			button2:SetPos( 159, 200 )
			button2:SetText( '' )
			button2.DoClick = function()
			SpawnPaint()
			end

And I only got this error after adding whatever you gave me.

What exactly is line 468? Doesnt look like it relates to my code…


draw.RoundedBox( 0, 0, 0, w, h, Color( 212, 0,  ) )

Fixed it.

Now I have this error:



[ERROR] gamemodes/sandbox/gamemode/cl_init.lua:357: Calling net.Start with unpooled message name [http://goo.gl/qcx0y]
  1. Start - [C]:-1
   2. SpawnPaint - gamemodes/sandbox/gamemode/cl_init.lua:357
    3. DoClick - gamemodes/sandbox/gamemode/cl_init.lua:450
     4. unknown - lua/vgui/dlabel.lua:206


Here’s line 357:


net.Start( "spawn_paint" )

Did you remember to have this on SERVER SIDE?

[lua]
// Server side
util.AddNetworkString(“spawn_paint”)
net.Receive(“spawn_paint”, function(len, client)
if !client:HasWeapon(“weapon_spraymhs”) then
client:Give(“weapon_spraymhs”)
end
end)
[/lua]