Roblox Chasing NPC Script (Smart PathfindingService)
Finds the nearest player within detection range and computes obstacle-avoiding paths using PathfindingService.
-- Script inside an NPC Rig Model
local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")
local npc = script.Parent :: Model
local humanoid = npc:WaitForChild("Humanoid") :: Humanoid
local root = npc:WaitForChild("HumanoidRootPart") :: BasePart
local DETECT_RANGE = 60
local function nearestTarget(): BasePart?
local best: BasePart? = nil
local bestDist = DETECT_RANGE
for _, p in ipairs(Players:GetPlayers()) do
local tr = p.Character and p.Character:FindFirstChild("HumanoidRootPart") :: BasePart?
local th = p.Character and p.Character:FindFirstChildOfClass("Humanoid")
if tr and th and th.Health > 0 then
local d = (tr.Position - root.Position).Magnitude
if d < bestDist then bestDist = d; best = tr end
end
end
return best
end
task.spawn(function()
while humanoid.Health > 0 do
local target = nearestTarget()
if target then
local path = PathfindingService:CreatePath({ AgentCanJump = true })
path:ComputeAsync(root.Position, target.Position)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
if waypoints[2] then
if waypoints[2].Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end
humanoid:MoveTo(waypoints[2].Position)
end
end
end
task.wait(0.25)
end
end)How to install in Studio
- Insert an R15 Rig in Workspace (make sure HumanoidRootPart is NOT anchored).
- Insert a Script inside the NPC Model and paste the code.
- Press Play and walk within 60 studs to watch the NPC navigate around obstacles toward you.
Tips & customization
- Call `root:SetNetworkOwner(nil)` at the top so the server owns the NPC smoothly without stuttering when players pass by.
Common questions.
Why isn't my Roblox NPC moving?
Check that none of the NPC's parts (especially HumanoidRootPart) have Anchored = true.
More free Luau scripts.
LocalScript
Shift-to-Sprint Script
Hold Left Shift to sprint smoothly with a subtle camera FieldOfView boost and clean speed reset on release.
Copy script →
LocalScript
Double Jump Script
Lets players jump a second time mid-air using Roblox's JumpRequest event (works on PC, mobile, and controller).
Copy script →
Script
Kill Brick Script
Instead of copying 100 scripts into 100 lava bricks, tag any part 'KillBrick' and let one clean server script handle all of them.
Copy script →
Script
Round System Script
A clean server loop that runs Intermission -> Active Round -> Winner announcement and updates a shared Status StringValue for your HUD.
Copy script →
Skip manual wiring.
Let Revix code & playtest it.
Start building freeFree to start. No card needed.
