Disabling Fall damage when SWEP is in hands

I am making a SWEP that increases the players jump power when the left click and sets it back to normal when they right click. After doing this I wanted to disable fall damage when the SWEP is in the hands of the player. I have found this: http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index5fc8.html and I got this code out of it:


function SWEP:Deploy()
	self.Owner.ShouldReduceFallDamage = true
	return true
end
 
function SWEP:Holster()
	self.Owner.ShouldReduceFallDamage = false
	return true
end
 
local function ReduceFallDamage(ent, inflictor, attacker, amount, dmginfo)
	if ent:IsPlayer() and ent.ShouldReduceFallDamage and dmginfo:IsFallDamage() then
		dmginfo:SetDamage(1)
	end
end
 
hook.Add("EntityTakeDamage", "ReduceFallDamage", ReduceFallDamage)
 

I placed this at the bottom of my SWEP shared.lua script.

I get this error every time I hit the ground after jumping really high:


[Gravity Minimizer] lua/weapons/gravity minimizer/shared.lua:69: attempt to index local 'dmginfo' (a nil value)
  1. v - lua/weapons/gravity minimizer/shared.lua:69
   2. unknown - lua/includes/modules/hook.lua:84


Anyone have any ideas on how to fix this error? Also it does not turn off fall damage. Which should be common sense.

I think you should change the “local function ReduceFallDamage” to this:


SWEP:Think()
local Newdamage=dmginfo:GetDamage
if ent:IsPlayer() and self.Owner.ShouldReduceFallDamage = true and dmginfo:IsFallDamage() then
		dmginfo:SetDamage(1)
elseif ent:IsPlayer() and self.Owner.ShouldReduceFallDamage = false and dmginfo:IsFallDamage() then
		dmginfo:SetDamage(Newdamage)


I believe that should work.

Why do you disagree lolcats?

I should replace this:


local function ReduceFallDamage(ent, inflictor, attacker, amount, dmginfo)
	if ent:IsPlayer() and ent.ShouldReduceFallDamage and dmginfo:IsFallDamage() then
		dmginfo:SetDamage(1)
	end
end

with this, correct?:


function SWEP:Think()
local Newdamage=dmginfo:GetDamage
if ent:IsPlayer() and self.Owner.ShouldReduceFallDamage = true and dmginfo:IsFallDamage() then
		dmginfo:SetDamage(1)
elseif ent:IsPlayer() and self.Owner.ShouldReduceFallDamage = false and dmginfo:IsFallDamage() then
		dmginfo:SetDamage(Newdamage)
	end
end

Edit: Does not work

Because that’s not how you code. In any language. Ever. :v:



if (SERVER) then
    local function nofalldam(target, dmginfo)    
        if (dmginfo:IsFallDamage() and target:GetActiveWeapon():GetClass() == "your_swep_name_here") then
            dmginfo:SetDamage(0)
        end
        return dmginfo
    end
    hook.Add("EntityTakeDamage", "donotfalldamage", nofalldam)
end


Ok this is my code currently:


//This makes sure clients download the file
	AddCSLuaFile()

//How heavy the SWEP is
	SWEP.Weight = 1
 
//Allow automatic switching to/from this weapon when weapons are picked up
	SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false

	SWEP.PrintName = "Jump Enhancer"
	SWEP.Slot = 1
	SWEP.SlotPos = 3
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
	
	SWEP.Author = "Praesdynamite"
	SWEP.Contact = "amrcommunity.com"
	SWEP.Purpose = "Increases Jump Height"
	SWEP.Instructions = "Right click to jump higher. Left click to set to normal"
	SWEP.Category = "Praesdynamite's Schit Scripts"
	SWEP.Spawnable = true -- Whether regular players can see it
	SWEP.AdminSpawnable = true -- Whether Admins/Super Admins can see it

	SWEP.ViewModel = "models/weapons/v_hands.mdl" -- What the player with the gravity minimizer sees
	SWEP.WorldModel = "models/weapons/w_hands.mdl" -- What other players see.
	
	//Left click information
	SWEP.Primary.ClipSize 		= -1
	SWEP.Primary.DefaultClip 	= -1
	SWEP.Primary.Ammo 			= "none"
	SWEP.Primary.Delay          = 10
	SWEP.Primary.Automatic = false
	
	//Right click stuff
	SWEP.Secondary.ClipSize = -1
	SWEP.Secondary.DefaultClip = -1
	SWEP.Secondary.Automatic = false
	SWEP.Secondary.Ammo = "none"
	
	
local GravitySound = Sound("sound/weapons/gravity minimizer/gravitychange.wav")
	

	
	
function SWEP:PrimaryAttack()
	self.Owner:SetJumpPower(1000)
	self.Owner:EmitSound(GravitySound)
	
end

function SWEP:SecondaryAttack()
	self.Owner:SetJumpPower(200)
	self.Owner:EmitSound(GravitySound)
end


////---This is the bit you told me to put in. It does not work, though there are no script errors.
if (SERVER) then


local function nofalldam(ply, hitgroup, dmginfo)
			
			if (dmginfo:IsFallDamage() and ply:GetActiveWeapon():GetClass() == "gravity_minimizer") then
				dmginfo:SetDamage(0)
			end
		
	end
	hook.Add("ScalePlayerDamage", "donotfalldamage", nofalldam)
	
end



[editline]29th August 2014[/editline]

I still take 10 health damage when I hit the ground

Use my edited code I posted above. Apparantly ScalePlayerDamage doesn’t detect fall damage (reasonable).

I have been frustrated with this addon all day. Thank you it worked.

To reduce fall damage you’d need to use the hook for it, not EntityScaleDamage because ALL damage will be removed OR you’re adding additional logic checks that aren’t necessary.


//
// Logic to give a SWEP a perk of reducing fall damage to 0 - Josh 'Acecool' Moser
// When we use a hook, make sure we only return data for our specific logic
//

//
// Remove Fall-Damage if holding a special swep...
//
hook.Add( "GetFallDamage", "SwepRemovesFallDamage", function( _p, _fallspeed )
	if ( !IsValid( _p ) ) then return; end

	// Make sure the weapon is valid, and that it is a swep
	local _w = _p:GetActiveWeapon( );
	if ( !IsValid( _w ) || !_w.Clip1 ) then return; end

	// Make sure the swep table exists..
	local _tab = _w:GetTable( );
	if ( !istable( _tab ) ) then return; end

	// Now, if SWEP.KillFallDamagePERK = true; then we have 0 fall damage.
	if ( _tab.KillFallDamagePERK ) then
		return 0;
	end
end );

In your SWEP, set SWEP.KillFallDamagePERK = true; for weapons that you should have 0 falldamage with.

https://dl.dropboxusercontent.com/u/26074909/tutoring/weapons/weapon_perks.lua.html
Hopefully these tutorials and information helps. As always, to view the Lua from any of my tutorials to enable copy/pasting ( HTML doesn’t copy/paste well in terms of HTML / CSS Highlighted Lua ), remove .html from the url.