Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do I edit this pathfinding function of mine so that it works on high walkspeed humanoids?

Asked by 6 days ago

To get it to path find consistently to a changing position you must spam it, but when it is at higher walkspeeds it messes up, I don't know why or how to fix it, please help:

modtable = {}
pathfindingcache = {}
function modtable.Humanoid:StartPathFind(hum, despoint)
        if pathfindingcache[hum:GetFullName()] then
            pathfindingcache[hum:GetFullName()].isdone = 1
            repeat game:GetService("RunService").Heartbeat:wait() until pathfindingcache[hum:GetFullName()] == nil
            pathfindingcache[hum:GetFullName()] = {}
            pathfindingcache[hum:GetFullName()].isdone = 0
        else
            pathfindingcache[hum:GetFullName()] = {}
            pathfindingcache[hum:GetFullName()].isdone = 0
        end
        if pathfinddebounce == 1 then return end
        if pcall(function() return despoint.IsA end) then
            despoint = despoint.Position
        end
        local PathfindingService = game:GetService("PathfindingService")
        local connect = {}

        local path = PathfindingService:CreatePath()
        local waypoints
        local currentWaypointIndex

        local function followPath()
            path = PathfindingService:CreatePath()
            path:ComputeAsync(hum.RootPart.Position, despoint)
            waypoints = {}

            if path.Status == Enum.PathStatus.Success then
                waypoints = path:GetWaypoints()
                currentWaypointIndex = 1
                hum:MoveTo(waypoints[currentWaypointIndex].Position)
                if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
                    hum.Jump = true
                end
            else
                hum:MoveTo(hum.RootPart.Position)
            end
     end

     local function onWaypointReached(reached)
            if pathfindingcache[hum:GetFullName()].isdone == 1 then return end
            if not waypoints or not currentWaypointIndex then return end
            if reached and #waypoints > currentWaypointIndex then 
                if waypoints[currentWaypointIndex] then
                    currentWaypointIndex = currentWaypointIndex + 1
                end
                local nocollide = {}
                for i,obj in ipairs(workspace:GetDescendants()) do
                    if pcall(function() return obj:IsA("BasePart") end) then
                    if obj:IsA("BasePart") and obj.CanCollide == false then
                        table.insert(nocollide,obj)
                    end
                    end
                end
                local function raycast(num,num2,tab2)
                    if waypoints[num + 1] == nil then return end
                    local ray = Ray.new(waypoints[num].Position, waypoints[num2].Position)
                    table.insert(tab2,waypoints[num])
                    local a
                    if pcall(function() return workspace:FindPartOnRayWithIgnoreList(ray,tab2) end) then
                        a = workspace:FindPartOnRayWithIgnoreList(ray,tab2)
                    else
                    end
                    if a then
                        return true
                    else return nil end
                end
                    if raycast(currentWaypointIndex,currentWaypointIndex + 1,nocollide) ~= nil then
                        hum:MoveTo(waypoints[currentWaypointIndex].Position)
                        if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
                            hum.Jump = true
                        end
                    elseif waypoints[currentWaypointIndex + 1] then
                        local origin = currentWaypointIndex
                        repeat
                            currentWaypointIndex = currentWaypointIndex + 1
                        until raycast(origin,currentWaypointIndex,nocollide) ~= true
                        if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
                            hum.Jump = true
                        end
                        hum:MoveTo(waypoints[currentWaypointIndex].Position)

                    elseif not waypoints[currentWaypointIndex] then
                        currentWaypointIndex = currentWaypointIndex - 1
                    end
            else
            end
        end

        local function onPathBlocked(blockedWaypointIndex)
            if pathfindingcache[hum:GetFullName()].isdone == 1 then return end
            if blockedWaypointIndex > currentWaypointIndex then
                followPath()
            end
        end

        connect["onPathBlocked"] = path.Blocked:Connect(onPathBlocked)

        connect["onWaypointReached"] = hum.MoveToFinished:Connect(onWaypointReached)
        local function check()
            if pathfindingcache[hum:GetFullName()].isdone == 1 then
                pathfinddebounce = 0
                connect["onPathBlocked"]:disconnect()
                connect["onWaypointReached"]:disconnect()
                                pathfindingcache[hum:GetFullName()] = nil
                connect["check"]:disconnect()
            end
        end
        connect["check"] = game:GetService("RunService").Heartbeat:Connect(check)
        followPath()
end
0
Is the issue when the NPC stops, it goes past it's destination due to momentum? If so, you'll probably need to attempt changing friction - if that even affects characters- or use a custom everything. If not those options, attempt finding the extra distance passed then subtract it from the destination alphawolvess 1301 — 6d
0
You know what? That is probally it, becuase I think it overshots it then trys to move back to it, which turns into a infinite back and forth dance routine, I'll try to figure it out on my own, but I might still need an example, could you provide one?. ben0h555 55 — 5d
0
Welp, I cant seem to figure out how to keep it from overshooting and stil retaining the speed. ben0h555 55 — 5d

Answer this question