How to stop npc from following path midway?
So I have a script, and I copied the pathfind script that follows it's enemy. And if it sees it's enemy it's gonna stop itself from walking to the target.
Well, that's what I want it to do, but it doesn't work.
Script (not full script):
01 | local ray 1 = Ray.new(script.Parent.M 16. Shoot.Position, (v.HumanoidRootPart.Position - script.Parent.M 16. Shoot.Position).Unit * 1000 ) |
02 | local hit 1 , position 1 = workspace:FindPartOnRayWithIgnoreList(ray 1 , { script.Parent } ) |
03 | if not hit 1 :IsDescendantOf(v) then |
04 | local Destination = v.HumanoidRootPart.Position |
05 | local Path = PathfindingService:CreatePath() |
06 | local NPC = script.Parent |
07 | local Humanoid = NPC.Humanoid |
08 | Path:ComputeAsync(NPC.PrimaryPart.Position, Destination) |
10 | local activeCoroutine = nil |
14 | coroutine.wrap( function () |
16 | if hit 1 :IsDescendantOf(v) then follow = false break end |
24 | if script.Parent.Humanoid.Health ~ = 0 then |
25 | Path:ComputeAsync(NPC.PrimaryPart.Position, Destination) |
28 | local Waypoints = Path:GetWaypoints() |
29 | table.remove(Waypoints, 1 ) |
31 | coroutine.wrap(followPath)(Waypoints) |
32 | if hit 1 :IsDescendantOf(v) then return end |
35 | function followPath(waypoints) |
36 | activeCoroutine = coroutine.running() |
37 | if hit 1 :IsDescendantOf(v) then return end |
38 | if hit 1 :IsDescendantOf(v) then coroutine.close(activeCoroutine) end |
39 | for _, point in ipairs (waypoints) do |
40 | if hit 1 :IsDescendantOf(v) then break end |
41 | if activeCoroutine~ = coroutine.running() then |
43 | Humanoid:MoveTo(point.Position) |
44 | Humanoid.MoveToFinished:Wait() |
45 | if point.Action = = Enum.PathWaypointAction.Jump then |
Please help