Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while true do
- wait()
- local PathfindingService = game:GetService("PathfindingService")
- local character = script.Parent
- local humanoid = character.Humanoid
- local root = humanoid.RootPart -- humanoid.RootPart is faster than character.HumanoidRootPart
- local destination = workspace.Destination2.Position -- use workspace, not game.Workspace
- local path = PathfindingService:CreatePath()
- local waypoints
- local currentWaypointIndex
- local function followPath(destinationobject)
- -- computer and check the path
- path:ComputeAsync(root.humanoid.RootPart.Position, destinationobject.PrimaryPart.Position)
- -- Empty waypoints table after each new computation
- waypoints = {}
- if path.Status == Enum.PathStatus.Success then -- We check if the path was successfull here
- local waypoints = path:GetWaypoints()
- for i = 1, #waypoints do -- Iterate through every waypoint
- local waypoint = waypoints[i]
- if waypoint.Action == Enum.PathWaypointAction.Walk then
- humanoid:MoveTo(waypoint.Position)
- humanoid.MoveToFinished:Wait()
- else
- humanoid.Jump = true
- end
- end
- -- End missing here?
- local function onWaypointReached(reached)
- if reached and currentWaypointIndex < #waypoints then
- currentWaypointIndex = currentWaypointIndex + 1
- humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
- end
- end
- local function onPathBlocked(blockedWaypointIndex)
- -- Check if the obstacle is further down the path
- if blockedWaypointIndex > currentWaypointIndex then
- -- Call function to re-compute the path
- followPath(destination)
- -- Connect 'Blocked' event to the 'onPathBlocked' function
- path.Blocked:Connect(onPathBlocked)
- -- Connect 'MoveToFinished' event to the 'onWaypointReached' function
- humanoid.MoveToFinished:Connect(onWaypointReached)
- followPath(destination)
- end
- end
- end
- end
- end -- Extra end?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement