GM:PlayerHurt, attacker:TakeDamage not working?


hook.Add("PlayerHurt", "traitortest", function(victim, attacker)
	if (inuse) then 
		if attacker == self.Owner and victim:GetRole() == ROLE_TRAITOR then 
			victim:TakeDamage(200, attacker, self)
		elseif attacker == self.Owner and victim:GetRole() != ROLE_TRAITOR then
			attacker:TakeDamage(200, attacker, self)
		end
	end
end)

if i kill a traitor, they take the damage like they’re supposed
but if i kill an innocent, the same thing happens and they also die (when by the above code, i should die, not them)

the weapon also does only 1 damage so it’s not the weapon.

You are trying to use self outside a SWEP function, it doesn’t work that way. You could instead do something like this


hook.Add("PlayerHurt", "traitortest", function(victim, attacker)
	if (inuse) and IsValid(attacker:GetActiveWeapon()) and attacker:GetActiveWeapon():GetClass() == "what ever class it is" then 
		if victim:GetRole() == ROLE_TRAITOR then 
			victim:TakeDamage(200, attacker, self)
		elseif victim:GetRole() != ROLE_TRAITOR then
			attacker:TakeDamage(200, attacker, self)
		end
	end
end)

now it’s doing absolutely nothing and damaging everyone for 1 dmg regardless of who they are


hook.Add("PlayerHurt", "traitortest", function(victim, attacker)
	if (inuse) and IsValid(attacker:GetActiveWeapon()) and attacker:GetActiveWeapon():GetClass() == "weapon_ttt_oneshot" then 
		if attacker == self.Owner and victim:GetRole() == ROLE_TRAITOR then 
			victim:TakeDamage(200, attacker, self)
		elseif attacker == self.Owner and victim:GetRole() != ROLE_TRAITOR then
			attacker:TakeDamage(200, attacker, self)
		end
	end
end)

(also i just switched weapon bases from default to TTT but that doesn’t change the fact that it doesn’t work)

[editline]13th October 2015[/editline]

so uh i fixed it and did this


hook.Add("PlayerHurt", "traitortest", function(victim, attacker)
	if (inuse) and IsValid(attacker:GetActiveWeapon()) and attacker:GetActiveWeapon():GetClass() == "weapon_ttt_oneshot" then 
		if victim:GetRole() == ROLE_TRAITOR then 
			victim:TakeDamage(200, attacker, self)
		elseif victim:GetRole() != ROLE_TRAITOR then
			attacker:TakeDamage(200, attacker, self)
		end
	end
end)

and my server disconnnected me with


Disconnect: de_stroyer overflowed reliable buffer


when i shot someone

That’s because its causing an indefinite loop. TakeDamage fires the PlayerHurt hook. If you looking to simply kill the player you could try using:Kill(). Or you can try overriding what ever function shoots the bullets and apply the damage in the bullet callback function http://wiki.garrysmod.com/page/Entity/FireBullets

got it working using ply:Kill, thanks.