Pathfinding Support

So this is the first time I’m using pathfinding in my entire lua development journey.

I need this to update the target in realtime (Kind of like a nextbot) How would I go about doing this?

function movetoTarget()
    local enemy = Stack:GetAttribute("Target")
    local path = game:GetService("PathfindingService"):CreatePath()
    path:ComputeAsync(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, game.Workspace.Game[enemy].HumanoidRootPart.Position)

    if path.Status == Enum.PathStatus.Success then
        print("Finding path")
        local wayPoints = path:GetWaypoints()

        for i = 1, #wayPoints do

            local point = wayPoints[i]
            local human = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")

            local marker = Instance.new("Part")
            marker.Size = Vector3.new(1, 1, 1)
            marker.Position = point.Position
            marker.Anchored = true
            marker.BrickColor = BrickColor.new("Bright blue")
            marker.CanCollide = false
            marker.Parent = game.Workspace

        
            if human then
                human:MoveTo(point.Position)
                local success = human.MoveToFinished:Wait()

                if not success then
                    print("trying again")
                end
            else
                print("Humanoid not found in character.")
                break
            end
        end
    else
        warn("Path not created. Trying again.")
    end
end

You can compute a path every chosen interval that looks realistic without causing too much lag. For example, you could compute a path every 0.25 seconds. However, let’s say you have 100 NPCs. With that many paths being created, you’d want to increase the interval to maybe every 0.75 seconds. Additionally, you may also want to consider simply using MoveTo() and directly going towards the target if the path has only 1 waypoint, since MoveTo() is less resource intensive and is equal to computing the path with a single waypoint. There are other ways to optimize pathfinding further, but this is just a simple suggestion.

If I use MoveTo, could I still update that 1 waypoint in realtime?

If you use MoveTo() while the humanoid is currently moving, the previous movement will be canceled and replaced with the new destination.

This is just how I do it:

local pathfinding_service = game:GetService("PathFindingService")
local function find_target()
local target = nil --can change name

--insert code here

return target --return the target
end

while wait(1) do --Can change this number
local torso = find_target() --Getting the returned target
local path = pathfinding_service:CreatePath()
local success, errorMessage = pcall(function() --Even if there is an error, the code below this thread will still run.
   path:ComputeAsync(script.Parent.Torso.Position, torso.Position)
--if you want, you can have an animation play here
end)

if success and Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()

for _, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.WaypointAction.Jump then --The AI will jump over stuff it needs to.
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
script.Parent.Humanoid:MoveTo(waypoint.Position)
script.Parent.Humanoid.MoveToFinished:Wait(0.1) --Can change the wait time
end
end

This script is basically just a basic example of how you could use pathfinding for zombies and nextbots n’ all that stuff!
Hope this helps :cowboy_hat_face: