Need help with pathfinding script

Okay so basically i have this pathfinding script for an enemy right. It works perfectly for like 3-5 minutes but then randomly the enemy guy just starts limping or like stuttering. Ive tried messing around with the numbers in the script and even the startercharacter you play as but nothings worked. Although whenever i move the enemy while its limping it works again for like 5 seconds.


Heres a video of it

Also script

local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Character = script.Parent
local humanoid = Character:WaitForChild("Humanoid")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local function findTarget()
    local maxDistance = 500
    local nearestTarget

    for index, player in pairs(Players:GetPlayers()) do
        if player.Character then
            local target = player.Character
            local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

            if distance < maxDistance then
                nearestTarget = target
                maxDistance = distance
            end
        end
    end

    return nearestTarget
end

local function applyDamage()
    for index, player in pairs(Players:GetPlayers()) do
        if player.Character then
            local target = player.Character
            local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

            if distance < 5 then
                target.Humanoid:TakeDamage(0)
            end
        end
    end
end

local path = Pathfinding:CreatePath({
    AgentHeight = 6;
    AgentRadius = 3;
    AgentCanJump = false;

    Cost = {
        Water = 100;
        DangerZone = math.huge
    }
})

local function followPath(destination)
    local success, errormessage = pcall(function()
        path:ComputeAsync(Character.PrimaryPart.Position, destination)
    end)

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

        blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
            if blockedWaypointIndex >= nextWaypointIndex then
                blockedConnection:Disconnect()
                followPath(destination)
            end
        end)

        if reachedConnection then
            reachedConnection:Disconnect()
        end

        reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
            if reached and nextWaypointIndex < #waypoints then
                nextWaypointIndex += 1
                humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
            else
                reachedConnection:Disconnect()
                blockedConnection:Disconnect()
            end
        end)

        nextWaypointIndex = 2
        humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
    else
        warn("Path not contputetmed", errormessage)
    end
end

RunService.Heartbeat:Connect(function()
    applyDamage()
end)

while wait(0) do
    local target = findTarget()
    if target then
        followPath(target.HumanoidRootPart.Position)
    end
end
1 Like

i had same probelm with an animal and to fix it i just took the humanoid hipheight to 1

you alos might need to turn platformstand to true on the humanoid