The NPC stutters and eventually stops moving with PathfindingService
Script:
local function moveToPoint(enemy,Modde)
if Modde == "Defense" then
local success, errorMessage = pcall(function()
Path:ComputeAsync(char.PrimaryPart.Position, enemy.HumanoidRootPart.Position - CFrame.new(char.HumanoidRootPart.Position, enemy.HumanoidRootPart.Position).LookVector * 8 )
end)
if success and Path.Status == Enum.PathStatus.Success then
wayPoints = Path:GetWaypoints()
blockedConn = Path.Blocked:Connect(function(blockedWaypoint)
if blockedWaypoint >= nextWaypoint then
blockedConn:Disconnect()
moveToPoint(enemy)
end
end)
if not reachedConn then
reachedConn = char.Humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypoint < #wayPoints then
nextWaypoint += 1
char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
else
reachedConn:Disconnect()
blockedConn:Disconnect()
end
end)
end
nextWaypoint = 2
char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
else
warn("Path Blocked!",errorMessage)
end
elseif Modde == "Offense" then
local success, errorMessage = pcall(function()
Path:ComputeAsync(char.PrimaryPart.Position, enemy.HumanoidRootPart.Position - CFrame.new(char.HumanoidRootPart.Position, enemy.HumanoidRootPart.Position).LookVector * 2.5 )
end)
if success and Path.Status == Enum.PathStatus.Success then
wayPoints = Path:GetWaypoints()
blockedConn = Path.Blocked:Connect(function(blockedWaypoint)
if blockedWaypoint >= nextWaypoint then
blockedConn:Disconnect()
moveToPoint(enemy)
end
end)
if not reachedConn then
reachedConn = char.Humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypoint < #wayPoints then
nextWaypoint += 1
char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
else
reachedConn:Disconnect()
blockedConn:Disconnect()
end
end)
end
nextWaypoint = 2
char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
else
warn("Path Blocked!",errorMessage)
end
elseif Modde == "None" then
local success, errorMessage = pcall(function()
Path:ComputeAsync(char.PrimaryPart.Position, enemy.HumanoidRootPart.Position)
end)
if success and Path.Status == Enum.PathStatus.Success then
wayPoints = Path:GetWaypoints()
blockedConn = Path.Blocked:Connect(function(blockedWaypoint)
if blockedWaypoint >= nextWaypoint then
blockedConn:Disconnect()
moveToPoint(enemy)
end
end)
if not reachedConn then
reachedConn = char.Humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypoint < #wayPoints then
nextWaypoint += 1
char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
else
reachedConn:Disconnect()
blockedConn:Disconnect()
end
end)
end
nextWaypoint = 2
char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
else
warn("Path Blocked!",errorMessage)
end
end
end
while true do
if not movementCD then
if lastMode == nil or "Offense" then
local moveDecider = math.random(1,2)
if moveDecider == 1 then
mode = "Defense"
lastMode = mode
moveToPoint(enemy,mode)
coroutine.wrap(function()
task.wait(5)
movementCD = false
end)()
elseif moveDecider == 2 then
mode = "Offense"
lastMode = mode
moveToPoint(enemy,mode)
coroutine.wrap(function()
task.wait(5)
movementCD = false
end)()
end
elseif lastMode == "Defense" then
mode = "Offense"
lastMode = mode
moveToPoint(enemy,mode)
coroutine.wrap(function()
task.wait(5)
movementCD = false
end)()
end
end
task.wait(0.1)
end