Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Pathfinding finishing old path before starting new one?

Asked by 3 years ago
--//PathfindHandler
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Torso = script.Parent:WaitForChild("Torso")
local Animation = Instance.new("Animation")
Animation.AnimationId = "http://www.roblox.com/asset/?id=180426354"
local LoadedAnimation = Humanoid:LoadAnimation(Animation)
LoadedAnimation:Play()
while true do
    local targetName = script.Parent:WaitForChild("Target")
    local PathService = game:GetService("PathfindingService")
    local Target = workspace:WaitForChild(targetName.Value)
    local StartPos = Torso.Position
    local EndPos = Target.Torso.Position
    local path = PathService:FindPathAsync(StartPos, EndPos)
    local Waypoints = path:GetWaypoints()
    for _, waypoint in pairs(Waypoints) do
        Humanoid:MoveTo(waypoint.Position)
        Humanoid.MoveToFinished:Wait(2)
    end
        wait()
end

The script finishes the entire path before making a new one. And when my character teleports back to a starting location on touch of the npc, it no longer goes to the npc it just walks around aimlessly. So I was wondering if there was a way to cancel the in pairs loop and go for a new waypoint so I dont have this issue.

Wondering if anyone knows the issue, thanks!

Answer this question