Skip to content

OnPlayerDamage

The server accepted a hit on a player - change the damage, cancel it, or let it through.

Called before the damage comes off the health. attacker is the pid who struck, or -1 for damage without one (an NPC actor’s blow when the mode has no OnActorAttack, SetPlayerHealth). zone is the attack zone the attacker’s game recorded; part the body part struck, 1 .. 6 (0 unknown; GetBodyPartName); weapon the attacker’s weapon by its table name (shortswordBroad, bow_c + arrow_normal; "" bare-handed or without the tables); dtype "stab", "slash", "smash" or "". Return a number to change the damage (0 cancels the hit, so do a stamina cost, an injury and a bleed), false to cancel it, nothing to keep it. Hits between teammates never get here.

function OnPlayerDamage(pid, attacker, damage, zone, part, weapon, dtype)
-- ...
end
Parameter Type
pid number the victim
attacker number who struck; -1 = nobody
damage number what the victim is about to lose
zone number the attack zone the attacker’s game recorded
part number the body part struck, 1 .. 6; 0 unknown
weapon string the weapon’s table name; "" bare-handed
dtype string "stab", "slash", "smash" or ""

a number replaces the damage (0 cancels); false cancels the hit; nothing keeps it

-- no damage in the lobby, double to the head, a log line for a big one
function OnPlayerDamage(pid, attacker, damage, zone, part, weapon, dtype)
if IsPlayerInZone(pid, lobby) then return false end
if part == BODY_PART_HEAD then damage = damage * 2 end
if damage > 40 and attacker >= 0 then
Log(string.format("%s hit %s for %.0f with %s (%s)", GetPlayerName(attacker), GetPlayerName(pid), damage, weapon, dtype))
end
return damage
end

OnPlayerInjury · OnPlayerDeath · OnActorDamage · GetBodyPartName · SetPlayerTeam · the Combat group of the index