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")
end)


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.
Ergo:


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

Edit:

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
// ARGS: PLAYER pl, NUMBER key
// AUTH: Entoros

if SERVER then
	concommand.Add("cl_keypress",function(pl,cmd,args)
		local n = tonumber(args[1])
		hook.Call("PlayerKeyPress",GAMEMODE,pl,n)
	end)
else
	local keys_pressed = {}
	hook.Add("Think","CheckKeyPress",function()
		for i =1, 130 do
			if input.IsKeyDown( i ) then
				if keys_pressed* then return end
				RunConsoleCommand( "cl_keypress", i )
				hook.Call("PlayerKeyPress",GAMEMODE,LocalPlayer(),i)
				keys_pressed* = true
			elseif keys_pressed* then keys_pressed* = nil
			end
		end
	end)
end


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 )

hook.Add("PlayerButtonDown","Bind",function(ply,key)
   if(ply:Alive())then
	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)
                end
        end     
   end

end)


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 = {
{

"tfa_acr",
"tfa_ak47",
"tfa_ak74",
"tfa_amd65",
"tfa_an94",
"tfa_auga3",
"tfa_fal",
"tfa_g36",
"tfa_g3a3",
"tfa_l85",
"tfa_m14sp",
"tfa_m16a4_acog",
"tfa_m416",
"tfa_scar",
"tfa_tar21",
"tfa_val",
"tfa_winchester73",
"tfa_mp5",
"tfa_dragunov",
"tfa_honeybadger",
"tfa_intervention",
"tfa_m98b",
"tfa_mp5sd",
"tfa_aw50",
"tfa_bizonp19",
"tfa_usc"

}
}

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

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

end)


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?
add



if SERVER then
   stuffHere
end


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.