Need help making a gun not let owner get any fall damage while holding it

I’ve been trying to make the player have no fall damage while holding a specific weapon, I have tried every kind of tutorial and wiki info I’ve found around the internet, none of them seem to work anymore…

i’ve also tried adding to the weapon’s shared.lua


function GM:GetFallDamage( ply )     if ply:GetActiveWeapon() == "weapon" then	         return 0     else return 10     end end
To shared.lua





function GAMEMODE:GetFallDamage() 	if ply:GetActiveWeapon():GetClass() == "weapon_pistol" then 		return 0 	end end

Nothing Worked!

You can do something like this:
[lua]
if CLIENT then return end
local CFallWeapons = {
[“weapon_pistol”] = 10,
[“weapon_physgun”] = 0
}

hook.Add(“Initialize”, “OverrideGMHooks”, function()
function GAMEMODE:GetFallDamage(ply, speed)
local CFDamage = CFallWeapons[ply:GetActiveWeapon():GetClass()]
if CFDamage then
return CFDamage
end
return 10
end
end)
[/lua]

dont do it like that…

use this instead:

[lua]
if CLIENT then return end

local tblFallWeapons = {
“weapon_pistol”,
“weapon_physgun”
}

hook.Add(“GetFallDamage”, “OverrideGMHooks”, function(ply, speed)
if (table.HasValue(tblFallWeapons, ply:GetActiveWeapon():GetClass())) then
return 0;
end

return GAMEMODE:GetFallDamage(ply, speed);

end)
[/lua]

Both above solutions are unnecessarily complicated and also do it in a really silly way.

brandonj4’s solution is useful if you want to make it so different weapons give you different fall damage, however the way he completely replaces GetFallDamage when the gamemode initializes is really weird and there is absolutely no reason for you to do that.

G4MB!T’s solution hooks into GetFallDamage properly, however he uses table.HasValue, which is really inefficient. You should really avoid using that function.

Here’s a solution that looks more like what you originally tried to do.

[lua]
hook.Add(“GetFallDamage”, “NoFallDamage”, function(ply, dmg)
local w = ply:GetActiveWeapon()
if w:IsValid() and w:GetClass() == “weapon_pistol” then
return 0
end
end)
[/lua]

You should use hook.Add to catch gamemode events instead of overwriting the gamemode function itself. The second argument to hook.Add is a unique name that is used to identify your hook, this way you can have multiple hooks on the same gamemode event.
GetActiveWeapon returns a weapon entity, not its class name, that’s why you need to use GetClass to get the actual name of the weapon.

That’s very informative! I wanna thank all of you for the help, I’m going to try it and give you a feedback as soon a possible c:

By the way, I made a typo on the second line, it’s ply:GetActiveWeapon(), not pl:GetActiveWeapon(). Fixed it on my post.

Also, something you might want to know about hooks: when there are several functions hooked to a gamemode event, here’s what happens. When a player is about to receive fall damage, the game starts by calling every function hooked to GetFallDamage, one after each other, in an arbitrary, unpredictable order. If a function returns nil or nothing, it goes to the next one, until it finds one that returns a value.

If none of them return anything, it calls the function from GM (if there is one).

That’s why I returned 0 only if the player is holding a pistol in that function. Otherwise, nothing is returned and the gamemode’s default behaviour is used (10 fall damage).

Still not working, I placed your code into the terrortown/gamemode/init.lua file… the weapon I’m trying to make this code work with is “weapon_ttt_lightsaber”
So I used your code like this


hook.Add("GetFallDamage", "NoFallDamage", function(ply, dmg)
	local w = pl:GetActiveWeapon()
	if w:IsValid() and w:GetClass() == "weapon_ttt_lightsaber" then
		return 0
	end
end)

to

**or **

to


function SWEP:Initialize( )
  self.BaseClass:Initialize( )
  hook.Add( "GetFallDamage", self, self.GetFallDamage )
end

function SWEP:GetFallDamage( pl )
  if pl:GetActiveWeapon( ) == self then
    return 0
  end
end

As another solution, however untested it may be.

thank you very much brandon, dumb me, but it doesn’t work : /

[editline]27th May 2013[/editline]

I’ll try that

[editline]27th May 2013[/editline]

It gave me this error "
[ERROR] gamemodes/terrortown/entities/weapons/weapon_tttbase/shared.lua:466: attempt to index field ‘Weapon’ (a nil value)

  1. Initialize - gamemodes/terrortown/entities/weapons/weapon_tttbase/shared.lua:466
  2. unknown - gamemodes/terrortown/entities/weapons/weapon_ttt_lightsaber/shared.lua:209

"

[editline]27th May 2013[/editline]

dont work

[lua]self.BaseClass.Initialize( self )[/lua]

didnt work either :C

Stop confusing him further for fucks sake, it’s already obvious he’s rather new to Lua so you don’t have to post awfully complex or tricky solutions and then have him wonder why it doesn’t work.

That’s… rather original. It’s not going to work though, self.GetFallDamage is going to be called without self as the first argument, and even if it worked, the hook would stay after the weapon is removed.


hook.Add("GetFallDamage", "NoFallDamage", function(ply, dmg)
	local w = ply:GetActiveWeapon()
	if w:IsValid() and w:GetClass() == "weapon_ttt_lightsaber" then
		return 0
	end
end)

This is the code I posted previously. It must work. It’s the most straightforward way to do it. If it really doesn’t work, then try this:


hook.Add("GetFallDamage", "NoFallDamage", function(ply, dmg)
	return 0
end)

This should make you never take fall damage. If you still take fall damage, then you did something wrong. It’s a good idea to enable the developer console and use it to see Lua errors that might occur. Because if your code has errors, nothing you put in there is going to work.

Are those my lightsabers?

Idk, I got those lightsabers models from garrysmod.org, and fixed them, plus added some functions to them… I kinda scripted most of it.

I’ll test your code killburn! thanks

[editline]28th May 2013[/editline]

No lua errors :c I wonder if the TTT has some override over fall damage, wich would make it necessary for us to edit it in gamemodes file or something like that.
Ps: No lua errors in developers console

terrortown/gamemode/player.lua



-- The GetFallDamage hook does not get called until around 600 speed, which is a
-- rather high drop already. Hence we do our own fall damage handling in
-- OnPlayerHitGround.
function GM:GetFallDamage(ply, speed)
   return 1
 end

And it only took me 5 seconds to open the files and seach, so you better do that yourself next time.

thanks for the info, but if you read my first post you will see that i’ve searched a lot before making this thread, you prob searched for the function itaelf and found that, I will work on that part of player.lua and see if I get anywhere

I used another hook that would analise every damage given to ents, it would check wether or not it was a player, and if his active weapon is the lightsaber he will have no fall damage, thanks anyway guys, solved.