• Trying to change TFA Recoil with lua
    13 replies, posted
I am trying to make it so that a TFA Test weapon ( That is a copy of another weapon with details changed so it loads ) recoil changes when the user crouches. Seperate file down below, I would put the TFA weapon file cause its no different from anyother tfa swep. I looked at another facepunch thread that had the same issue and i tried everything on there. if ( Entity(1):Crouching() == true ) then     Entity(1):GetActiveWeapon().Recoil = 0.5 elseif ( Entity(1):Crouching() == false ) then     Entity(1):GetActiveWeapon().Recoil = 100 end I also tried, if ( Entity(1):Crouching() == true ) then     Entity(1):GetActiveWeapon().Primary.Recoil = 0.5 elseif ( Entity(1):Crouching() == false ) then     Entity(1):GetActiveWeapon().Primary.Recoil = 100 end But neither worked, I also tried doing it in the weapon file it self doing the same checks.
Don't use Entity(1), weapon can be owned by any player so use self.Owner. Where is this code located? What function or something
Ok my code is located in garrysmod/lua/autorun/server/tfa_recoil_changing.lua And my whole code now reads, hook.Add( "KeyPress", "RecoilChange", function( ply, key )     if( key == IN_DUCK ) then         if ( self.Owner():GetActiveWeapon():GetClass() == "tfa_recoil_test" ) then             self.Owner():GetActiveWeapon().Recoil = 0.5         else return end     end end) hook.Add( "KeyPress", "RecoilRevert", function( ply, key )     if( key == IN_DUCK ) then         if ( self.Owner():GetActiveWeapon():GetClass() == "tfa_recoil_test" ) then             self.Owner():GetActiveWeapon().Recoil = 100         else return end     end end) However i now get this error in console, [ERROR] lua/autorun/server/tfa_recoil_changing.lua:11: attempt to index global 'self' (a nil value)   1. fn - lua/autorun/server/tfa_recoil_changing.lua:11    2. unknown - addons/ulib-v2_63/lua/ulib/shared/hook.lua:109
self.Owner is not a function. Programming in Lua
Ok i have changed the code to the following, hook.Add( "KeyPress", "RecoilChange", function( ply, key )     if( key == IN_DUCK ) then         if ( ply:GetActiveWeapon():GetClass() == "tfa_recoil_test" ) then             ply:GetActiveWeapon().Recoil = 0.5         else return end     end end) hook.Add( "KeyRelease", "RecoilRevert", function( ply, key )     if( key == IN_DUCK ) then         if ( ply:GetActiveWeapon():GetClass() == "tfa_recoil_test" ) then             ply:GetActiveWeapon().Recoil = 100         else return end     end end) And it works to an extent, It changes the recoil to 0.5 when crouching but when releasing it doesnt change it back.
Do not return anything if you don't know what to do. return will stop other hooks
Even after removing the returns it still doesnt change back
In the code above it won't, hooks only stop being called if something other than nil is returned.
if( key != IN_DUCK ) then try that on your release
Doesnt work, When changing it to != it breaks the code completely
Firstly, don't have several hooks for one method, pack it into one hook. Secondly, both the functions contradict each other by; They both return and check for the exact same code. Thirdly, these hooks are literally the same. hook.Add( "Think", "RecoilTest", function() for k, v in pairs( player.GetAll() ) do if v:GetActiveWeapon():GetClass() == "tfa_recoil_test" ) if v:Crouching() then v:GetActiveWeapon().Recoil = 0.5 else v:GetActiveWeapon().Recoil = 100 end end end end) I did this so you would understand ^. It must be a SHARED hook. As a SWEP table is shared. Also, why don't you just edit tfa_recoil_test instead of having all of these checks? You could just do what I did above but with SWEP:Think().
[ERROR] lua/weapons/tfa_gun_base/shared.lua:1449: attempt to call method 'Recoil' (a number value)   1. unknown - lua/weapons/tfa_gun_base/shared.lua:1449
Show your code, you have syntax error
You have no clue how the TFA base works, or so I'm led to believe. Recoil, spread, damage, and a plethora of other basic weapon statistics are "stat-cached" which means they can't be edited on-the-fly. Also, the base has this functionality in by default in most cases. And if it doesn't, you can add it yourself without this hook.
Sorry, you need to Log In to post a reply to this thread.