I’m currently working to create an NPC that chases and attacks players- for the most part, it’s working fine. Though when the NPC has a faster walkspeed than my character I get a sort of laggy result as seen blow:
This is the part of my script that handles the tracking and attacking:
local function makePath()
local newPath = PathFindingService:CreatePath()
newPath:ComputeAsync(Character.HumanoidRootPart.Position,target.Position)
local waypoints = newPath:GetWaypoints()
if self.DebugMode then
for _,Point in pairs(waypoints) do
local t = {
Size = Vector3.new(1,1,1),
Anchored = true,
CanCollide = false,
Material = Enum.Material.Neon,
["CFrame"] = Point.CFrame,
}
local newPart = makeDebugPart(t)
delay(3,function()
newPart:Destroy()
end)
end
end
if newPath.Status == Enum.PathStatus.Success then
print("made path")
for _,Point in pairs(waypoints) do
if self.Target == nil then
break
end
if Point.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid:MoveTo(Point.Position)
local timeout = Humanoid.MoveToFinished:Wait(1)
if not timeout then
Humanoid.Jump = true
makePath()
break
end
if checkView() then
repeat
Humanoid:MoveTo(target.Position)
print(self.Target,self.Disabled,getDist())
if self.Target and not self.Disabled and getDist() <= 5 then
print("hit")
end
wait(0.1)
if not self.Target then
break
end
until not checkView()
print("lost view")
if persist and self.Target and not self.Disabled then
makePath()
end
break
end
if (target.Position-waypoints[#waypoints].Position).Magnitude > 20 then
makePath()
break
end
end
else
print("failed to make path")
makePath()
end
end
-- core
coroutine.wrap(function()
self.Target = target
makePath()
end)()
Any help would be much appreciated!