Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make an NPC that follows a player, avoiding walls (Smart NPC)?

I need to make an enemy NPC that follows a player, but also avoids walls and obstacles. I want it so that it can't get stuck by a player, and keeps on moving.

5 comments

0

I was just about to comment that

0

^ qq

0

If this has been answered, please mark it as answered, thanks.

1 answer

I've used basic pathfinding before, here's something useful:

local service = game:GetService("PathfindingService")
local humanoid = script.Parent
local torso = script.Parent.Parent.Torso
local finishPosition = nil --Change this to the nearest players torso

local path = service:ComputeRawPathAsync(torso.Position, finishPosition, 512)
local points = path:GetPointCoordinates()
for p = 1, #points do
	repeat
		humanoid:MoveTo(points[p])
		humanoid.MoveToFinished:wait()
	until (points[p] - torso.Position).Magnitude <= 4
end

Have fun scripting!

This discussion is preserved as it was. New answers, comments, and votes are closed.