Player::taskCombat
Summary
Directs the specified ped to attack a target player. Combat behavior and response can be adjusted using combatFlags and threatResponseFlags.
Syntax
player.taskCombat(targetPed, combatFlags, threatResponseFlags);
Combat Flags
- 0: Standard combat behavior
- 16: Aggressively pursue and attack the target
- 33: Defensive stance, attacks only if provoked
- 128: Avoids direct engagement in combat
Threat Response Flags
- 0: Default threat response
- 1: Defensive response, only counters attacks
- 16: High aggression, prioritizes combat engagement
- 256: Ignores threats, remains passive
Required Arguments
- targetPed: The handle or object of the ped the player will attack.
- combatFlags: int - Adjusts how the attacker engages the target.
- threatResponseFlags: int - Modifies how the attacker reacts to threats.
Return Value
- undefined - This function does not return a value.
Example
In this example, an NPC is spawned and instructed to engage the nearest player using specific combat settings.
const targetPlayer = mp.players.local; // Get the local player as the target (example)
const npc = mp.peds.new(mp.game.joaat('mp_m_freemode_01'),targetPlayer.position, 0, 0); // Create a freemode ped at a position
if (targetPlayer) {
npc.taskCombat(targetPlayer.handle, 16, 16); // Task NPC to aggressively pursue and engage the player
mp.gui.chat.push(`NPC is now engaging in combat with ${targetPlayer.name}`);
} else {
mp.gui.chat.push("Target player not found.");
}