-snip- Doesnt matter
[editline]7th September 2015[/editline]
The code now:
[code]
local MultiKeyBinder = {};
MultiKeyBinder.Binds = {};
function MultiKeyBinder.Think()
local self = MultiKeyBinder;
local p = LocalPlayer();
for _,v in pairs(self.Binds) do
local pressed = true;
for _,key in pairs(v.Keys) do
if(not input.IsKeyDown(key)) then
pressed = false;
break;
end
end
if(pressed) then
if(not v.Pressed) then
if p:IsTyping() or vgui.CursorVisible() then
else
v.Pressed = true;
p:ConCommand(v.Command);
end
end
else
v.Pressed = nil;
end
end
end
hook.Add("Think","MultiKeyBinder.Think",MultiKeyBinder.Think);
function MultiKeyBinder.AddBind(cmd,...)
table.insert(MultiKeyBinder.Binds,
{
Keys = {...},
Command = cmd,
}
);
end
//BIND PRIMARYS
MultiKeyBinder.AddBind(CUserCmd:SelectWeapon( "tfa_acr" ),KEY_1)
MultiKeyBinder.AddBind("use tfa_ak47",KEY_1)
MultiKeyBinder.AddBind("use tfa_ak74",KEY_1)
MultiKeyBinder.AddBind("use tfa_amd65",KEY_1)
MultiKeyBinder.AddBind("use tfa_an94",KEY_1)
MultiKeyBinder.AddBind("use tfa_auga3",KEY_1)
MultiKeyBinder.AddBind("use tfa_fal",KEY_1)
MultiKeyBinder.AddBind("use tfa_g36",KEY_1)
MultiKeyBinder.AddBind("use tfa_g3a3",KEY_1)
MultiKeyBinder.AddBind("use tfa_l85",KEY_1)
MultiKeyBinder.AddBind("use tfa_m14sp",KEY_1)
MultiKeyBinder.AddBind("use tfa_m16a4_acog",KEY_1)
MultiKeyBinder.AddBind("use tfa_m416",KEY_1)
MultiKeyBinder.AddBind("use tfa_scar",KEY_1)
MultiKeyBinder.AddBind("use tfa_tar21",KEY_1)
MultiKeyBinder.AddBind("use tfa_val",KEY_1)
MultiKeyBinder.AddBind("use tfa_mp5",KEY_1)
MultiKeyBinder.AddBind("use tfa_dragunov",KEY_1)
MultiKeyBinder.AddBind("use tfa_honeybadger",KEY_1)
MultiKeyBinder.AddBind("use tfa_intervention",KEY_1)
MultiKeyBinder.AddBind("use tfa_m98b",KEY_1)
MultiKeyBinder.AddBind("use tfa_mp5sd",KEY_1)
MultiKeyBinder.AddBind("use tfa_aw50",KEY_1)
MultiKeyBinder.AddBind("use tfa_bizonp19",KEY_1)
MultiKeyBinder.AddBind("use tfa_usc",KEY_1)
//BIND SECONDARYS
MultiKeyBinder.AddBind(CUserCmd:SelectWeapon( "tfa_colt1911" ),KEY_2)
MultiKeyBinder.AddBind("use tfa_coltpython",KEY_2)
MultiKeyBinder.AddBind("use tfa_deagle",KEY_2)
MultiKeyBinder.AddBind("use tfa_glock",KEY_2)
MultiKeyBinder.AddBind("use tfa_hk45",KEY_2)
MultiKeyBinder.AddBind("use tfa_luger",KEY_2)
MultiKeyBinder.AddBind("use tfa_model3russian",KEY_2)
MultiKeyBinder.AddBind("use tfa_model627",KEY_2)
MultiKeyBinder.AddBind("use tfa_mp40",KEY_2)
MultiKeyBinder.AddBind("use tfa_sig_p229r",KEY_2)
MultiKeyBinder.AddBind("use tfa_tec9",KEY_2)
MultiKeyBinder.AddBind("use tfa_thompson",KEY_2)
MultiKeyBinder.AddBind("use tfa_usp",KEY_2)
MultiKeyBinder.AddBind("use tfa_uzi",KEY_2)
//BIND EXPLOSIVES
MultiKeyBinder.AddBind("use weapon_sh_grenade",KEY_3)
MultiKeyBinder.AddBind("use weapon_sh_c4",KEY_3)
MultiKeyBinder.AddBind("use weapon_sh_flash",KEY_3)
//BIND DOOR
MultiKeyBinder.AddBind("use weapon_sh_doormod",KEY_4)
//BIND TOOL GUN
MultiKeyBinder.AddBind(CUserCmd:SelectWeapon( "weapon_sh_tool" ),KEY_5)
[/code]
Only some of them are edited just to test, but it returns the error:
[ERROR] lua/autorun/client/keybinder.lua:44: attempt to index global 'CUserCmd' (a nil value)
1. unknown - lua/autorun/client/keybinder.lua:44
CUserCmd isn't a concommand, nor is it a table (atleast in _G)
CUserCmd isn't defined. You need to do it in a hook like CreateMove or StartCommand. You can also [URL="http://wiki.garrysmod.com/page/CUserCmd/SelectWeapon"]read the page on the command[/URL] and find this out as well. There's even an example that shows how to do it.
[QUOTE=AK to Spray;48633548]CUserCmd isn't defined. You need to do it in a hook like CreateMove or StartCommand. You can also [URL="http://wiki.garrysmod.com/page/CUserCmd/SelectWeapon"]read the page on the command[/URL] and find this out as well. There's even an example that shows how to do it.[/QUOTE]
I have also tried putting that example code at the top of my code and using
[code]
MultiKeyBinder.AddBind(LocalPlayer():SelectWeapon("tfa_acr",KEY_1)) --What I was supposed to put, according to the wiki
[/code]
but that just gave the error that SelectWeapon is a nil value
Using MultiKeyBinder.AddBind is going to only work for [b]console commands[/b] (like "use") not Lua functions, unless you change the code to do so. Player.SelectWeapon doesn't return a function either, if you want to run Player.SelectWeapon when a key is pressed, do something like this:
[code]
function MultiKeyBinder.Think()
local self = MultiKeyBinder;
local p = LocalPlayer();
for _,v in pairs(self.Binds) do
local pressed = true;
for _,key in pairs(v.Keys) do
if(not input.IsKeyDown(key)) then
pressed = false;
break;
end
end
if(pressed) then
if(not v.Pressed) then
if not (p:IsTyping() or vgui.CursorVisible()) then
if type(v.Command) == "function" then
v.Command()
elseif type(v.Command) == "string" then
LocalPlayer():ConCommand(v.Command)
end
end
end
else
v.Pressed = nil;
end
end
end
...
local function whatever()
LocalPlayer():SelectWeapon(weapon thing)
end
MultiKeyBinder.AddBind(whatever, some key)
[/code]
[QUOTE=stev_;48633775]Using MultiKeyBinder.AddBind is going to only work for [b]console commands[/b] (like "use") not Lua functions, unless you change the code to do so. Player.SelectWeapon doesn't return a function either, if you want to run Player.SelectWeapon when a key is pressed, do something like this:
[code]
function MultiKeyBinder.Think()
local self = MultiKeyBinder;
local p = LocalPlayer();
for _,v in pairs(self.Binds) do
local pressed = true;
for _,key in pairs(v.Keys) do
if(not input.IsKeyDown(key)) then
pressed = false;
break;
end
end
if(pressed) then
if(not v.Pressed) then
if not (p:IsTyping() or vgui.CursorVisible()) then
if type(v.Command) == "function" then
v.Command()
elseif type(v.Command) == "string" then
LocalPlayer():ConCommand(v.Command)
end
end
end
else
v.Pressed = nil;
end
end
end
...
local function whatever()
LocalPlayer():SelectWeapon(weapon thing)
end
MultiKeyBinder.AddBind(whatever, some key)
[/code][/QUOTE]
Thankyou, do I need to do something like:
[code]
if(input.IsKeyDown(KEY_1))then
SELECT WEAPON
end
[/code]
inside the function whatever
No, as the function whatever only runs when key 'some key' is pressed.
[QUOTE=stev_;48633835]No, as the function whatever only runs when key 'some key' is pressed.[/QUOTE]
Oh I see
[editline]7th September 2015[/editline]
[code]
function MultiKeyBinder.Think()
local self = MultiKeyBinder;
local p = LocalPlayer();
for _,v in pairs(self.Binds) do
local pressed = true;
for _,key in pairs(v.Keys) do
if(not input.IsKeyDown(key)) then
pressed = false;
break;
end
end
if(pressed) then
if(not v.Pressed) then
if not (p:IsTyping() or vgui.CursorVisible()) then
if type(v.Command) == "function" then
v.Command()
elseif type(v.Command) == "string" then
LocalPlayer():ConCommand(v.Command)
end
end
end
else
v.Pressed = nil;
end
end
end
local function primary()
LocalPlayer():SelectWeapon( "tfa_acr" )
end
local function secondary()
LocalPlayer():SelectWeapon( "tfa_acr" )
end
local function explosive()
LocalPlayer():SelectWeapon( "tfa_acr" )
end
local function door()
LocalPlayer():SelectWeapon( "tfa_acr" )
end
local function tool()
if(LocalPlayer():HasWeapon("weapon_sh_tool"))then
LocalPlayer():SelectWeapon( "weapon_sh_tool" )
end
end
//PRIMARYS
MultiKeyBinder.AddBind(primary,KEY_1)
//SECONDARYS
MultiKeyBinder.AddBind(secondary,KEY_2)
//EXPLOSIVES
MultiKeyBinder.AddBind(explosive,KEY_3)
//DOOR
MultiKeyBinder.AddBind(door,KEY_4)
//TOOL GUN
MultiKeyBinder.AddBind(tool,KEY_5)
[/code]
-FIXED-
Merge the solution I posted with your current one.
Also:
Remove the if statement in here, it's pointless, Player.SelectWeapon already checks that.
[code]
local function tool()
if(LocalPlayer():HasWeapon("weapon_sh_tool"))then
LocalPlayer():SelectWeapon( "weapon_sh_tool" )
end
end
[/code]
[b]Edit:[/b]
You can still use concommands for MultiKeyBinder.AddBind, you can just also use functions with it.
[QUOTE=stev_;48633970]Merge the solution I posted with your current one.
Also:
Remove the if statement in here, it's pointless, Player.SelectWeapon already checks that.
[code]
local function tool()
if(LocalPlayer():HasWeapon("weapon_sh_tool"))then
LocalPlayer():SelectWeapon( "weapon_sh_tool" )
end
end
[/code]
[b]Edit:[/b]
You can still use concommands for MultiKeyBinder.AddBind, you can just also use functions with it.[/QUOTE]
THANKYOU SO MUCH! THIS WAY OF SWITCHING HAS SOLVED MY ISSUE!!
[editline]7th September 2015[/editline]
[QUOTE=stev_;48633970]Merge the solution I posted with your current one.
Also:
Remove the if statement in here, it's pointless, Player.SelectWeapon already checks that.
[code]
local function tool()
if(LocalPlayer():HasWeapon("weapon_sh_tool"))then
LocalPlayer():SelectWeapon( "weapon_sh_tool" )
end
end
[/code]
[b]Edit:[/b]
You can still use concommands for MultiKeyBinder.AddBind, you can just also use functions with it.[/QUOTE]
Sadly it isnt working with multiple switches in one function
[editline]7th September 2015[/editline]
[code]
local meta = FindMetaTable( "Player" )
function meta:SelectWeapon( class )
self.DoWeaponSwitch = self:GetWeapon( class )
end
hook.Add( "CreateMove", "WeaponSwitch", function( cmd )
if ( !IsValid( LocalPlayer().DoWeaponSwitch ) ) then return end
cmd:SelectWeapon( LocalPlayer().DoWeaponSwitch )
if ( LocalPlayer():GetActiveWeapon() == LocalPlayer().DoWeaponSwitch ) then
LocalPlayer().DoWeaponSwitch = nil
end
end )
local MultiKeyBinder = {};
MultiKeyBinder.Binds = {};
function MultiKeyBinder.Think()
local self = MultiKeyBinder;
local p = LocalPlayer();
for _,v in pairs(self.Binds) do
local pressed = true;
for _,key in pairs(v.Keys) do
if(not input.IsKeyDown(key)) then
pressed = false;
break;
end
end
if(pressed) then
if(not v.Pressed) then
if not (p:IsTyping() or vgui.CursorVisible()) then
if type(v.Command) == "function" then
v.Command()
elseif type(v.Command) == "string" then
LocalPlayer():ConCommand(v.Command)
end
end
end
else
v.Pressed = nil;
end
end
end
hook.Add("Think","MultiKeyBinder.Think",MultiKeyBinder.Think);
function MultiKeyBinder.AddBind(cmd,...)
table.insert(MultiKeyBinder.Binds,
{
Keys = {...},
Command = cmd,
}
);
end
local function primary()
LocalPlayer():SelectWeapon( "tfa_acr" )
LocalPlayer():SelectWeapon( "tfa_ak47" )
LocalPlayer():SelectWeapon( "tfa_ak74" )
LocalPlayer():SelectWeapon( "tfa_amd65" )
LocalPlayer():SelectWeapon( "tfa_an94" )
LocalPlayer():SelectWeapon( "tfa_auga3" )
LocalPlayer():SelectWeapon( "tfa_fal" )
LocalPlayer():SelectWeapon( "tfa_g36" )
LocalPlayer():SelectWeapon( "tfa_g3a3" )
LocalPlayer():SelectWeapon( "tfa_l85" )
LocalPlayer():SelectWeapon( "tfa_m14sp" )
LocalPlayer():SelectWeapon( "tfa_m16a4_acog" )
LocalPlayer():SelectWeapon( "tfa_m416" )
LocalPlayer():SelectWeapon( "tfa_scar" )
LocalPlayer():SelectWeapon( "tfa_tar21" )
LocalPlayer():SelectWeapon( "tfa_val" )
LocalPlayer():SelectWeapon( "tfa_mp5" )
LocalPlayer():SelectWeapon( "tfa_honeybadger" )
LocalPlayer():SelectWeapon( "tfa_intervention" )
LocalPlayer():SelectWeapon( "tfa_m98b" )
LocalPlayer():SelectWeapon( "tfa_mp5sd" )
LocalPlayer():SelectWeapon( "tfa_bizonp19" )
LocalPlayer():SelectWeapon( "tfa_usc" )
end
local function secondary()
LocalPlayer():SelectWeapon( "tfa_colt1911" )
LocalPlayer():SelectWeapon( "tfa_coltpython" )
LocalPlayer():SelectWeapon( "tfa_deagle" )
LocalPlayer():SelectWeapon( "tfa_glock" )
LocalPlayer():SelectWeapon( "tfa_hk45" )
LocalPlayer():SelectWeapon( "tfa_luger" )
LocalPlayer():SelectWeapon( "tfa_model3russian" )
LocalPlayer():SelectWeapon( "tfa_model627" )
LocalPlayer():SelectWeapon( "tfa_mp40" )
LocalPlayer():SelectWeapon( "tfa_sig_p229r" )
LocalPlayer():SelectWeapon( "tfa_tec9" )
LocalPlayer():SelectWeapon( "tfa_thompson" )
LocalPlayer():SelectWeapon( "tfa_usp" )
LocalPlayer():SelectWeapon( "tfa_uzi" )
end
local function explosive()
LocalPlayer():SelectWeapon( "weapon_sh_grenade" )
LocalPlayer():SelectWeapon( "weapon_sh_c4" )
LocalPlayer():SelectWeapon( "weapon_sh_flash" )
end
local function door()
LocalPlayer():SelectWeapon( "weapon_sh_doormod" )
end
local function tool()
LocalPlayer():SelectWeapon( "weapon_sh_tool" )
end
//PRIMARYS
MultiKeyBinder.AddBind(primary,KEY_1)
//SECONDARYS
MultiKeyBinder.AddBind(secondary,KEY_2)
//EXPLOSIVES
MultiKeyBinder.AddBind(explosive,KEY_3)
//DOOR
MultiKeyBinder.AddBind(door,KEY_4)
//TOOL GUN
MultiKeyBinder.AddBind(tool,KEY_5)
[/code]
[editline]7th September 2015[/editline]
U know what, ill make a function for each weapon :)
[editline]7th September 2015[/editline]
Only the flash,tool gun, door is working:
[code]
local meta = FindMetaTable( "Player" )
function meta:SelectWeapon( class )
self.DoWeaponSwitch = self:GetWeapon( class )
end
hook.Add( "CreateMove", "WeaponSwitch", function( cmd )
if ( !IsValid( LocalPlayer().DoWeaponSwitch ) ) then return end
cmd:SelectWeapon( LocalPlayer().DoWeaponSwitch )
if ( LocalPlayer():GetActiveWeapon() == LocalPlayer().DoWeaponSwitch ) then
LocalPlayer().DoWeaponSwitch = nil
end
end )
local MultiKeyBinder = {};
MultiKeyBinder.Binds = {};
function MultiKeyBinder.Think()
local self = MultiKeyBinder;
local p = LocalPlayer();
for _,v in pairs(self.Binds) do
local pressed = true;
for _,key in pairs(v.Keys) do
if(not input.IsKeyDown(key)) then
pressed = false;
break;
end
end
if(pressed) then
if(not v.Pressed) then
if not (p:IsTyping() or vgui.CursorVisible()) then
if type(v.Command) == "function" then
v.Command()
elseif type(v.Command) == "string" then
LocalPlayer():ConCommand(v.Command)
end
end
end
else
v.Pressed = nil;
end
end
end
hook.Add("Think","MultiKeyBinder.Think",MultiKeyBinder.Think);
function MultiKeyBinder.AddBind(cmd,...)
table.insert(MultiKeyBinder.Binds,
{
Keys = {...},
Command = cmd,
}
);
end
--=======================--
--PRIMARY FUNCTIONS--
--=======================--
local function primary1()
LocalPlayer():SelectWeapon( "tfa_acr" )
end
local function primary2()
LocalPlayer():SelectWeapon( "tfa_ak47" )
end
local function primary3()
LocalPlayer():SelectWeapon( "tfa_ak74" )
end
local function primary4()
LocalPlayer():SelectWeapon( "tfa_amd65" )
end
local function primary5()
LocalPlayer():SelectWeapon( "tfa_an94" )
end
local function primary6()
LocalPlayer():SelectWeapon( "tfa_auga3" )
end
local function primary7()
LocalPlayer():SelectWeapon( "tfa_fal" )
end
local function primary8()
LocalPlayer():SelectWeapon( "tfa_g36" )
end
local function primary9()
LocalPlayer():SelectWeapon( "tfa_g3a3" )
end
local function primary10()
LocalPlayer():SelectWeapon( "tfa_l85" )
end
local function primary11()
LocalPlayer():SelectWeapon( "tfa_m14sp" )
end
local function primary12()
LocalPlayer():SelectWeapon( "tfa_m16a4_acog" )
end
local function primary13()
LocalPlayer():SelectWeapon( "tfa_m416" )
end
local function primary14()
LocalPlayer():SelectWeapon( "tfa_scar" )
end
local function primary15()
LocalPlayer():SelectWeapon( "tfa_tar21" )
end
local function primary16()
LocalPlayer():SelectWeapon( "tfa_val" )
end
local function primary17()
LocalPlayer():Se
[code]
function meta:SelectWeapon( class )
if ( !self:HasWeapon( class ) ) then return end
self.DoWeaponSwitch = self:GetWeapon( class )
end[/code]
I sort of forgot about the "I don't have such weapon stuff".
[QUOTE=Robotboy655;48634780][code]
function meta:SelectWeapon( class )
if ( !self:HasWeapon( class ) ) then return end
self.DoWeaponSwitch = self:GetWeapon( class )
end[/code]
I sort of forgot about the "I don't have such weapon stuff".[/QUOTE]
Its fine, who doesn't make mistakes :)
You forgot about it? Didnt stev post it?
Thankyou so much for your help!
Issue resolved!
Sorry, you need to Log In to post a reply to this thread.