#suggestions for an npc
1 messages · Page 1 of 1 (latest)
That's a solid idea — and it’s definitely doable, even for someone who's still learning scripting! It’s not easy per se, but it’s one of those things that feels complicated at first, then clicks once you break it down.
Your NPC idea involves:
Path following – making the NPC walk along a set route.
Player detection – noticing when a player is nearby.
Attacking behavior – attacking if a player is within a certain range/line of sight.
Returning to patrol – going back to the path if no player is near.
Here's a simple breakdown of how you’d script it:
- Path Following
Store waypoints as parts in a folder (e.g., inside Workspace.Waypoints).
Use Humanoid:MoveTo() to move to each one.
When it reaches one, go to the next.
- Player Detection
Use a while loop with RunService.Heartbeat or task.wait().
Check GetPlayers() and measure distance using magnitude.
You could also use Raycast or GetPartsInPart() (with a hitbox) for vision-based detection.
- Attacking
When a player is detected, stop the patrol loop.
Trigger an attack animation, reduce the player’s health, etc.
Use a cooldown so it doesn't attack constantly.
- Return to Path
If no player is nearby for a few seconds, resume path-following.
Is it hard?
If you know basic scripting (loops, functions, events) — it’s challenging but a great learning experience.
Once you do it once, you'll understand a lot more about NPC AI in Roblox.