GetActiveWeapon error door damage system



local doors = {
	
	"prop_door",
	"prop_door_rotating",		
	"func_door",
	"func_door_rotating"
}

local blocked = {
	"weapon_cc_stunstick",
	"weapon_cc_beanbag",
	"weapon_cc_pipe",
	"weapon_cc_hands",
	"weapon_cc_knife",
	"weapon_cc_manhack",
	"weapon_cc_molotov",
	"weapon_cc_flashbang",
	"weapon_cc_vortbroom",
	"weapon_cc_vortigaunt_slave",
	"weapon_cc_flare"
}

hook.Add("EntityTakeDamage","DoorsOpenWhenShot",function(target, dmginfo)
	print("takedamage")
	print(target:GetClass())
	if table.HasValue(doors,target:GetClass()) then
		if table.HasValue(blocked,dmginfo:GetInflictor():GetActiveWeapon():GetClass()) then 
			return
		end
		if target.damage then
			target.damage =  target.damage + dmginfo:GetDamage()
		else
			target.damage = dmginfo:GetDamage()
		end

		if target.damage > 300 then
			target:Input("Unlock",dmginfo:GetInflictor())
			target:Input("Open",dmginfo:GetInflictor())

		end
	end
end)


    

I get this error when I try to shoot on door with blocked weapon :



[ERROR] addons/damage_door_open/lua/autorun/server/dooropendamage.lua:28: attempt to call method 'GetActiveWeapon' (a nil value)
  1. v - addons/damage_door_open/lua/autorun/server/dooropendamage.lua:28
   2. unknown - lua/includes/modules/hook.lua:84
    3. FireBullets - [C]:-1
     4. ShootBullet - gamemodes/combinecontrol/entities/weapons/weapon_cc_base/shared.lua:730
      5. PrimaryUnholstered - gamemodes/combinecontrol/entities/weapons/weapon_cc_base/shared.lua:561
       6. unknown - gamemodes/combinecontrol/entities/weapons/weapon_cc_base/shared.lua:742


The code is in the server side.

This inflictor has a chance of being a weapon. Check the entity type before calling its weapon.

Do you mean


if table.HasValue(blocked,dmginfo:GetActiveWeapon():GetInflictor():GetClass()) then 

?

No. I mean check if the inflictor is a player or weapon before running a GetActiveWeapon check.



		if( dmginfo:GetInflictor() or dmginfo:IsWeapon() ) then
			if table.HasValue(blocked,dmginfo:GetInflictor():GetActiveWeapon():GetClass()) then 
				return
			end
		end


Error :



L 07/12/2015 - 14:16:28: Lua Error: 
[ERROR] addons/damage_door_open/lua/autorun/server/dooropendamage.lua:29: attempt to call method 'GetActiveWeapon' (a nil value)
  1. v - addons/damage_door_open/lua/autorun/server/dooropendamage.lua:29
   2. unknown - lua/includes/modules/hook.lua:84
    3. DispatchTraceAttack - [C]:-1
     4. PrimaryUnholstered - gamemodes/combinecontrol/entities/weapons/weapon_cc_base/shared.lua:674
      5. unknown - gamemodes/combinecontrol/entities/weapons/weapon_cc_base/shared.lua:742


Change it to


		if( dmginfo:GetInflictor():IsPlayer() ) then

Why not just use

CTakeDamageInfo:GetAttacker instead of GetInflictor? You still need to check if it the result is valid and is a player, though.

Also, it would be better to have the classes you want as keys in the table and check if the value is non-nil at that key instead of having the classes as values and using table.HasValue.




hook.Add("EntityTakeDamage","DoorsOpenWhenShot",function(target, dmginfo)
	print("takedamage")
	print(target:GetClass())
	if table.HasValue(doors,target:GetClass()) then
		
		if( dmginfo:GetAttacker():IsPlayer() ) then
			if table.HasValue(blocked,dmginfo:GetAttacker():GetActiveWeapon():GetClass()) then 
				return
			end
		end
		if target.damage then
			target.damage =  target.damage + dmginfo:GetDamage()
		else
			target.damage = dmginfo:GetDamage()
		end

		if target.damage > 300 then
			target:Input("Unlock",dmginfo:GetInflictor())
			target:Input("Open",dmginfo:GetInflictor())

		end
	end
end)



Line 28 :


if( dmginfo:GetAttacker():IsPlayer() ) then

That clearly isn’t line 28