My Command wont give player a weapon

Hey i have a code to where if you click a button it will give you a weapon/weapons but it will not give the weapon. There are no errors but it wont give the weapon!?
Here is the code where the button is

    		local DermaButton = vgui.Create( "DButton" )
    		DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel"
    		DermaButton:SetText( "Assault" )
    		DermaButton:SetPos( 25, 50 )
    		DermaButton:SetSize( 150, 50 )
    		DermaButton.DoClick = function ( ply )
    		print( "class1" )

Then that command gives the weapon

    //Console Commands
    concommand.Add( "class1", vcg_assault )
    //Commands Functions
    function vcg_assault()
    	ply:Give( "weapon_cs_m4" )

But it will not give the weapon. The weapon is in the entities/weapons folder.

Thanks for all the help :slight_smile:

  1. always localize functions that aren’t being used outside of the file.

local function vcg_assault(ply)
    ply:Give( "weapon_cs_m4" )

  1. I don’t know what you were trying to achieve with print. Use RunConsoleCommand()

DermaButton.DoClick = function ( ply )

3a. Do things in logical order to prevent errors.

local function vcg_assault(ply)
    ply:Give( "weapon_cs_m4" )

concommand.Add( "class1", vcg_assault )

3b. Instead of creating a separate function, you can do:

concommand.Add( "class1", function(ply, cmd, args)

Well printing and running a console command are completely different. Also your functions shouldn’t be global. As well be safe and register the console command under the function definition.

Edit: Apparently posting first snipping then editing your post to reflect information provided already means your not late. Sorry everyone.

You both made good points. And looking over i realized the dumb mistake of doing print. xD But thanks you guys :slight_smile: Ill check to see if i can fix the problem now :slight_smile:

[editline]30th August 2014[/editline]

With 3a. I thought the creation of the function had to be first? So its created…then used…not used then created. But idk i might be wrong, also 3b Seems nice but i like everthing organized, though that makes it simpler, but its easier for me to understand my way if i need to go back. I may used combining of creating and using function INSIDE the function. But for right now i like the way i make it. But thanks for the help and advise :slight_smile:

Your way is not standard and ugly. You really should learn the right way if your going to learn. Also pls don’t embed functions inside each other unless completely necessary. It’s really ugly.

Im thinking your meaning the 3a for your first statement. If so may i ask why, because my brain thinks that, you should create something before you use it. Because you cant use something thats not created first. Thats just how i think. Dont think im being a smart-ass :stuck_out_tongue: . If there is a logical reasoning why it should be “use then create”, i would love to know why :slight_smile: Thank :slight_smile:

function something()

IS CREATING the function. When you do concommand.Add(), you are then “binding” that function to the console command.

I changed my post because I had silly code in there that I didn’t want the OP to get misled by. Sorry. Have a rainbow. <3

Ok i understand you, and ok dont know what you ment toward crazy :stuck_out_tongue:

[editline]30th August 2014[/editline]

Ok the funniest thing just happened xD. Ok it worked ( thanks ) but it gave me the gun with me holding on my left, and wont shoot, and when i press right mouse it like shakes like crazy xD Can someone please tell me wtf thats all about lol :stuck_out_tongue:

Do you have CS:S mounted right.

Not sure what you mean mounted? Ive had problems with using CSS anyway. I could use the M9K Packs right? If so how would i go about that. Like add it do addons then… Thanks btw :slight_smile:

EDIT: Nvm i realized that is was to simple xD Thanks tho you guys for all your help :slight_smile: I know ill be back later but i think i may be ok for awhile :slight_smile:

Search in google, “how to mount css for garrysmod”. Literally the first link. If your using a hosting provider contact them and have them mount it.