Hello i am making a enemy NPC that follows you at a range, but the pathfinding is buggy and by that i mean it stops at every waypoint for a split second before continuing. Any tips would be appreciated, here is the script.
the wp are parts in my baseplate.
local Myhuman = script.Parent:WaitForChild("Humanoid")
local Mytorso = script.Parent:WaitForChild("Torso")
local me = script.Parent
function FindTarget()
local target = nil
local dist = 60
for i, v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v.Name ~= script.Parent.Name and v ~= script.Parent and human.Health > 0 then
if (Mytorso.Position - torso.Position).magnitude < dist then
dist = (Mytorso.Position - torso.Position).magnitude
target = torso
end
end
end
return target
end
while wait(1) do
local torso = FindTarget()
if torso then
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(Mytorso.Position, torso.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
Myhuman:ChangeState(Enum.HumanoidStateType.Jumping)
end
Myhuman:MoveTo(waypoint.Position)
Myhuman.MoveToFinished:Wait(0.5)
end
else
Myhuman:MoveTo(workspace.wp1.Position)
Myhuman.MoveToFinished:Wait(0.1)
Myhuman:MoveTo(workspace.wp2.Position)
Myhuman.MoveToFinished:Wait(0.1)
Myhuman:MoveTo(workspace.wp3.Position)
Myhuman.MoveToFinished:Wait(0.1)
end
end