ply:Give help.

I made a gun menu in my new gamemode, and its giving me some errors. Here’s the code:
[LUA] function guns_menu()

Gframe = vgui.Create( “DFrame” )
Gframe:SetPos( 50, 50)
Gframe:SetSize( 450, 290 ) //Set the size
Gframe:SetTitle( “Choose Gun” ) //Set title
Gframe:SetVisible( true )
Gframe:SetDraggable( false )
Gframe:ShowCloseButton( true )
Gframe:MakePopup()
Gframe:ShowCloseButton( false )
Gframe:SetDraggable( true )

local GIcon1 = vgui.Create( “SpawnIcon”, Gframe)
GIcon1:SetPos( 20, 40 )
GIcon1:SetModel( “models/weapons/w_rif_ak47.mdl” )
GIcon1:SetText( “AK47” )
GIcon1.DoClick = function( ply )
ply:Give(“weapon_ak47”)
Gframe:SetVisible( false )
end

end [/LUA]

Anyway, I’m going to add more guns, but the problem is this. -



[GGAirsoft\gamemode\cl_init.lua:107] attempt to call method 'Give' (a nil value)

How do I fix this?

You have no reference to the player. “ply” does not exist. Pass it in through an argument via con command or usermessage

I just tried adding

[LUA] local ply = LocalPlayer() [/LUA] to my code to reference to the player. No luck, how can I fix this exactly?

Its because you can’t give a person a weapon clientside.

You’re going to need to make the client run a console command that communicates with the server saying “give me this weapon”- but be careful as people can spoof console commands.

That would be kickass to be able to give weapons clientside, imagine the exploitation.

EDIT: So the OP understands how to do this

[lua]
if SERVER then
local function weaponHandler(ply, cmd, args)
local weapon = args[1]
ply:Give(args)
end
concommand.Add(“giveweapon”, weaponHandler)
end
[/lua]

Try this, untested but should work

[lua]if CLIENT then
function guns_menu()

	Gframe = vgui.Create( "DFrame" ) 
	Gframe:SetPos( 50, 50)
	Gframe:SetSize( 450, 290 ) //Set the size 
	Gframe:SetTitle( "Choose Gun" ) //Set title 
	Gframe:SetVisible( true ) 
	Gframe:SetDraggable( false ) 
	Gframe:ShowCloseButton( true ) 
	Gframe:MakePopup() 
	Gframe:ShowCloseButton( false )
	Gframe:SetDraggable( true )
	  
	local GIcon1 = vgui.Create( "SpawnIcon", Gframe) 
	GIcon1:SetPos( 20, 40 )
	GIcon1:SetModel( "models/weapons/w_rif_ak47.mdl" ) 
	GIcon1:SetText( "AK47" )
	GIcon1.DoClick = function( ply )
		RunConsoleCommand("ConsoleNameHere")
		Gframe:SetVisible( false )
	end
end

else
concommand.Add(“ConsoleNameHere”, function(ply)
ply:Give(“weapon_ak47”)
end)
end[/lua]

Severed, that’d work if it was ran shared. It’s fine.
Also, is GFrame not suppose to be global?

I know… but generally you wanna separate Client and Server, not just throw all the shit in shared and seperate it with server/client clauses. Not to mention if he’s worried about people spoofin’ concommands everywhere… Dig through the Cache and BAM. That’s why I hit the disagree button. Meant to post why, but got distracted with work.

I tried running this in shared but it didn’t work. How can I have the menu clientside and the ply:Give serverside?

the very first line…

This allows the game to run the appropriate code. If it is the client, lines 2 - 23 will be run. If the server calls this code, lines 25 - 27 will be run.

[editline]10th July 2012[/editline]

Also, if that “does not work” then you are doing something wrong. That code is as simple as it can get, and there are no errors.

Try putting this at the top of the file. Above the “if CLIENT then”
[lua]
IncludeClientFile(“ThisFilesDirectory/ThisFilesName.lua”)
[/lua]

Obviously change the file path so that it fits yours

The function doesn’t return a player, it returns a spawn icon.

This is a clientside VGUI screen, thus it means player’s can’t use “:Give()” function because its server only.

Instead use something called concommands.

[lua]concommand.Add( “somecommand”, function( ply, cmd, args )
if not args[1] then return end

ply:Give( args[1] )

end )[/lua]

to call the command with lua, on a client, you can use RunConsoleCommand(), you will need to put a comma in between every argument, thus:

RunConsoleCommand( “somecommand”, “weapon_ak47” )

You should make sure to check the weapons given in somecommand, or else someone could use the command to give them all types of admin or restricted weapons.

^ Way to sum up everything that has already been said.