RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"the npc should get within 5 units of the player before attempting to attack"
In the `attackPlayer` function, you can add a condition to check if the NPC is within 5 units of the player before attempting to attack. If the NPC is not within 5 units of the player, it will continue moving towards the player until it is within range. Here's how you can modify the function: ```lua -- Function to attack a player local function attackPlayer(player) isAttacking = true npc.Humanoid:MoveTo(player.Character.HumanoidRootPart.Position) while (player.Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude > 5 do wait(0.1) -- wait for a short time before checking the distance again end npc.Humanoid:LoadAnimation(npc.Animations.AttackAnimation):Play() wait(2) -- attack cooldown if (player.Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude < 5 then player.Character.Humanoid:TakeDamage(10) end isAttacking = false end ``` This will ensure that the NPC gets within 5 units of the player before attempting to attack.