Why does the pathfinding randomly stop they take a few seconds to run again, how do I make it continuously follow the target unless they are out of its range
local simplepath = require(game:GetService("ReplicatedStorage").SimplePath)
local players = game:GetService("Players")
local task = {}
local SUCCESS,FAIL,RUNNING = 1,2,3
function task.start(obj)
print("starting chase")
if not obj.model.PrimaryPart then
obj.model.PrimaryPart = obj.humanoid.RootPart
end
obj.path = simplepath.new(obj.model)
obj.path.Visualize = true
obj.isPathRunning = false
end
function task.finish(obj, status)
if obj.path then
if obj.path.Status == simplepath.StatusType.Active then
obj.path:Stop()
obj.isPathRunning = false
wait(0.5)
end
obj.path:Destroy()
obj.path = nil
end
obj.Blackboard.target = nil
end
function task.run(obj)
print("Running Chaser")
wait(0.5)
local Blackboard = obj.Blackboard
local target = obj.Target
local attackrange = obj.AttackRange
local targetvalue = obj.model
if target then
local targetcharacter = target
local targetHumanoid = targetcharacter:FindFirstChild("Humanoid")
local zombiehp = obj.model.HumanoidRootPart
local TargetHp = target.HumanoidRootPart
if not targetHumanoid or targetHumanoid.Health <=0 then
task.finish(obj, FAIL)
return FAIL
end
local distancetotarget = (zombiehp.Position - TargetHp.Position).Magnitude
print(distancetotarget)
if distancetotarget <= attackrange then
print("Npc is close")
task.finish(obj, SUCCESS)
return SUCCESS
end
if distancetotarget > obj.range then
task.finish(obj, FAIL)
return FAIL
end
if not obj.isPathRunning then
local success = obj.path:Run(TargetHp.Position)
if success then
obj.isPathRunning = true
obj.path.Reached:Once(function()
obj.isPathRunning = false
end)
obj.path.Blocked:Once(function()
obj.isPathRunning = false
end)
else
task.finish(obj, FAIL)
return FAIL
end
end
if obj.humanoid.Health <= 0 then
task.finish(obj, FAIL)
return FAIL
end
while obj.isPathRunning do
wait(0.1)
if obj.humanoid.Health <= 0 then
task.finish(obj, FAIL)
return FAIL
end
obj.path:Run(TargetHp.Position)
distancetotarget = (zombiehp.Position - TargetHp.Position).Magnitude
if distancetotarget <= attackrange then
print("Npc is close")
task.finish(obj, SUCCESS)
return SUCCESS
end
end
return SUCCESS
else
return FAIL
end
end
return task