#suggestions for an npc

1 messages · Page 1 of 1 (latest)

marsh wharf
#

So i know how to script but im not that good at it, i’ve never tried to script an npc is that hard? I want to make an npc that follows a path, he attacks if he sees a player, if the player is not near him anymore he will continue his path, what do u think

digital mauve
#

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:

  1. 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.

  1. 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.

  1. 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.

  1. 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.