Binding Key To Primary And Secondary

Hi, I am trying to bind keys with some sweps I added, I have overriden CHudWeaponSelection, as I didnt want it so im not sure if that is stopping my autorun script.

hook.Add("PlayerInitialSpawn", "Bind Keys", function()
    RunConsoleCommand("Bind 1 SWEP.Primary")
    RunConsoleCommand("Bind 2 SWEP.Secondary")

that was a bit of guess code, but atleast it gives you the right idea of what I want to do

Arguments for console commands should be passed as arguments.

RunConsoleCommand( "cmd", "arg", "arg" )

Thankyou, but do you know of a way to make it so it binds it to a primary weapon rather the slot

Well, bind function isn’t for binding weapons anyways.
It’s for binding a specific key to a specific console command.

I am certain that there isn’t a command such as ‘bind 1 weapon_class’.

bind 1 “use weapon_pistol”

Okay, I was wrong - thanks!

That sounds like just what I need, but is there a way to get what primary weapon I have?

[editline]30th August 2015[/editline]

-bump- I have tried:

weapons.Get( self.CurrentPrimary )
weapons.Get( self.CurrentSecondary )

but those dont seem to return anything, it is in a playerspawn hook

also you cant bind with runconsolecommand, i tried that a year ago. it blocks it.

ok, so I have an idea, but how would I go about binding with lua as I read another post and it seems to not be as simple as ply:ConCommand

You can’t bind keys without an unrestricted concommand module, even then the client would need it


Well, I mean you can’t run commands like “bind” or “unbind”.

I’m sure you could use input.IsKeyDown in a hook like KeyPress or something

this is the code that was on the thread I mentioned:

// ########################
// HOOK: PlayerKeyPress
// DESC: Serverside hook that's call when a player presses a key
// AUTH: Entoros

if SERVER then
		local n = tonumber(args[1])
	local keys_pressed = {}
		for i =1, 130 do
			if input.IsKeyDown( i ) then
				if keys_pressed* then return end
				RunConsoleCommand( "cl_keypress", i )
				keys_pressed* = true
			elseif keys_pressed* then keys_pressed* = nil

Why don’t you just use

GM/PlayerButtonDown instead of that? It pretty much does the exact same thing, and better.

so, am I right in thinking:

primary = weapons.Get( self.CurrentPrimary )
secondary = weapons.Get( self.CurrentSecondary )

	if (key == KEY_1 or key == KEY_PAD_1) then
            ply:ConCommand("use", primary)
                elseif(key == KEY_2 or key == KEY_PAD_2)then
                    ply:ConCommand("use", secondary)


I have tested the code and it gives me the error:

[ERROR] lua/autorun/client/bind.lua:14: ‘)’ expected (to close ‘(’ at line 4) near ‘end’

  1. unknown - lua/autorun/client/bind.lua:0

So, after testing stuff, I have this:

primaryList = {



hook.Add("PlayerSpawn", "Selects Weapons", function(ply)

    for k,primary in pairs( primaryList )do
         ply:HasWeapon( primary )
         print("Variable Primary:"..primary.."!")


so, then I was going to do it so when key 1 is pressed it runs the command use primary, as primary should be whatever weapon the player has. is this correct? also, it is not printing to the console.

are you sure it runs serverside?

if SERVER then

im running it clientside

tbh this is really pissing me off now, im not on about you or any other people, I mean I cant figure it out, there is literally no information online and I havnt done lua in ages so ive forgotten everything, so im not asking for people to write the code for me, just a point in the right direction, such as pseudo code etc…

Just run the code serverside and it’ll work.

PlayerSpawn hook is serverside, as you can see it on wiki.
So you cant run it clientside, unless you use networking, or some magic.