while wait(2.8) do path = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512) -- raw pathc = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512):GetPointCoordinates() for i,v in pairs(pathc) do local Part = Instance.new("Part", Droid.Head) Part.CFrame=CFrame.new(v) Part.Transparency = 0 Part.Color = Color3.new(0.2, 0, 1) Part.Size = Vector3.new(1, 1, 1) Part.Anchored = true Part.CanCollide = false Part.Locked = true Part.Name = 'Path' Part.Touched:connect(function(hit) Part:Destroy() end) if path.Status == Enum.PathStatus.Success then Part.Color = Color3.new(0, 1, 0) -- green elseif path.Status == Enum.PathStatus.ClosestNoPath then Part.Color = Color3.new(1, 0, 0) -- red elseif path.Status == Enum.PathStatus.ClosestOutOfRange then Part.Color = Color3.new(0.5, 0.75, 0) -- yellow elseif path.Status == Enum.PathStatus.FailStartNotEmpty then Part.Color = Color3.new(0.5, 0, 0.5) -- purple elseif path.Status == Enum.PathStatus.FailFinishNotEmpty then Part.Color = Color3.new(0.5, 0, 0.5) -- purple end Debris:AddItem(Part, 20.8) end for i, v in pairs(pathc) do Humanoid:MoveTo(v) repeat distance = (v - Droid.Head.Position).magnitude wait() until distance < 2 end end
Basically what the above script does is compute a path, make a part trail over it, and use a for loop + repeat loop to make a droid NPC follow the path.
The problem is the repeat loop yields the entire while loop, so it wont try to compute another path until it's gotten to the target of the first one.
This'll sound very requesty, but bear with me as I have no idea how to do this without breaking it.
What i need it to do is be able to make the droid stop following the path it's on without breaking the loop, compute a new path to PathTarget, then make the droid follow the new path.
What you can do is store the path you want it to be following in a variable. When the path changes, it will overwrite the other path. You can utilize this to compare the path the FollowPath function is using with the desired path and break the loop if they do not match.
local currentPath local function FollowPath(path, coordinates) for i, v in pairs(coordinates) do Droid.Humanoid:MoveTo(v) local distance repeat if path ~= currentPath then break -- If there is a new path, break this loop end distance = (v - Droid.Head.Position).magnitude wait() until distance < 2 end end while wait(2.8) do path = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512) currentPath = path coordinates = currentPath:GetPointCoordinates() -- Your Path Visualization Code spawn(function() FollowPath(path, coordinates) end) end
There is a neat coroutine method provided by Roblox named Spawn
which will run your code on a separate thread, almost like creating a brand new script. Using this, we can make the repeat loop run on a different thread.
while wait(2.8) do path = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512) -- raw pathc = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512):GetPointCoordinates() for i,v in pairs(pathc) do local Part = Instance.new("Part", Droid.Head) Part.CFrame=CFrame.new(v) Part.Transparency = 0 Part.Color = Color3.new(0.2, 0, 1) Part.Size = Vector3.new(1, 1, 1) Part.Anchored = true Part.CanCollide = false Part.Locked = true Part.Name = 'Path' Part.Touched:connect(function(hit) Part:Destroy() end) if path.Status == Enum.PathStatus.Success then Part.Color = Color3.new(0, 1, 0) -- green elseif path.Status == Enum.PathStatus.ClosestNoPath then Part.Color = Color3.new(1, 0, 0) -- red elseif path.Status == Enum.PathStatus.ClosestOutOfRange then Part.Color = Color3.new(0.5, 0.75, 0) -- yellow elseif path.Status == Enum.PathStatus.FailStartNotEmpty then Part.Color = Color3.new(0.5, 0, 0.5) -- purple elseif path.Status == Enum.PathStatus.FailFinishNotEmpty then Part.Color = Color3.new(0.5, 0, 0.5) -- purple end Debris:AddItem(Part, 20.8) end for i, v in pairs(pathc) do Humanoid:MoveTo(v) spawn(function() repeat distance = (v - Droid.Head.Position).magnitude wait() until distance < 2 end)) end end