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

Why are my NPC's stuttering when using pathfinding service?

Asked by 6 years ago

Hello, i am using pathfinding service to move my npcs around the town following a path of positions one have another.

When the game starts everything runs smooth, but a little later on the npcs start stuttering, like they get to the next waypoint and then its like they have to think about where the next one is, so they move a step, stop, move, stop, move stop, etc.

this script is in the npc model

local pathFindingService = game:GetService('PathfindingService')

local humanoid = script.Parent.Humanoid
local rootPart = script.Parent.HumanoidRootPart

local function moveToPoint(point)
    --Find a path to the destination and get the waypoints for the path
    local path = pathFindingService:FindPathAsync(rootPart.Position, point)
    local wayPoints = path:GetWaypoints()

    --Loop through all of the waypoints in the path
    for currentPoint = 1, #wayPoints do
        --Get the next waypoint to move to and move the humanoid towards it
        local waypoint = wayPoints[currentPoint]
        humanoid:MoveTo(waypoint.Position)

        --Check the action of the next waypoint. If it is a jump waypoint
        --then make the NPC jump
        if waypoint.Action == Enum.PathWaypointAction.Jump then
            humanoid.Jump = true
        end

        --wait until the NPC has reached the next waypoint
        local reachedTarget = humanoid.MoveToFinished:Wait()
    end
end







while wait(7) do    
    moveToPoint(game.Workspace.wg3.pos2.Position)   
    moveToPoint(game.Workspace.wg3.pos3.Position)   
    moveToPoint(game.Workspace.wg3.pos4.Position)
    moveToPoint(game.Workspace.wg3.pos5.Position)
    moveToPoint(game.Workspace.wg3.pos6.Position)   
    moveToPoint(game.Workspace.wg3.pos7.Position)
    moveToPoint(game.Workspace.wg3.pos8.Position)
    wait(7) 
    moveToPoint(game.Workspace.wg3.pos7.Position)
    moveToPoint(game.Workspace.wg3.pos6.Position)
    moveToPoint(game.Workspace.wg3.pos5.Position)
    moveToPoint(game.Workspace.wg3.pos4.Position)
    moveToPoint(game.Workspace.wg3.pos3.Position)
    moveToPoint(game.Workspace.wg3.pos2.Position)
    moveToPoint(game.Workspace.wg3.pos1.Position)                       
end

Answer this question