Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Could I Make It So An Enemy Can Accurately Follow The Player Using Pathfinding?

Asked by 5 years ago

I'm trying to make an enemy that follows the player, the problem I'm having is that when the enemy is chasing the player, the pathfinding script will create a path, and will have to finish walking that path before making a new one, which the result can be found here.

This is my current script:

local service = game:GetService("PathfindingService")
local humanoid = script.Parent.Humanoid
local torso = script.Parent.HumanoidRootPart

while true do
    wait()
local finishPosition = game.Workspace.MovingPart -- Player to follow
if finishPosition then
local path = service:ComputeRawPathAsync(torso.Position, finishPosition.Position, 512)
local points = path:GetPointCoordinates()
for p = 1, #points do
        humanoid:MoveTo(points[p])
        humanoid.MoveToFinished:Wait()
end
end
end

How could I make it so the enemy follows the player even if the player moves? Thanks!

0
ComputeRawPathAsync is deprecated, use FindPathAsync instead. User#19524 175 — 5y

Answer this question