#Pathfinding problem (solved)

1 messages · Page 1 of 1 (latest)

ocean comet
#

I want my pathfinding to be buttery smooth but it kind of sucks. I tried swapping the run service for task.wait but then the npc just finishes the whole path before continuing to chase which is obviously not good ._.

Any help very much appreciated, please ping me if you msg, thank you!

river trellis
#

do setnetworkownership(nil) on the humanoid

ocean comet
#

Helped reduce some stutter but the overall pathfinding is still problematic in terms of speed.

I tried rewriting with the task.wait loop but the npc just finishes an entire path before starting the next one, which is not what I want:

    local ClosestPlayer = nil
    local ClosestDistance = nil

    for _, player in pairs(Players:GetPlayers()) do
        local char = player.Character 
        if not char then continue end
        local hrp = char:FindFirstChild("HumanoidRootPart")
        if not hrp then continue end
        if not Rig then return end
        
        if char:GetAttribute("InRestZone") then continue end
        
        local distance = (hrp.Position - Rig.PrimaryPart.Position).Magnitude
        if not ClosestDistance then
            ClosestDistance = distance
        end
        
        if distance <= MaxDistance and distance <= ClosestDistance then
            ClosestDistance = distance
            ClosestPlayer = player
        end
    end
    
    if ClosestPlayer == nil then return end
    
    return ClosestPlayer, ClosestDistance
end  ```
#
    while task.wait(0.2) do
        local ClosestPlayer, ClosestDistance = FindClosestPlayer()
        if not ClosestPlayer then continue end
        local char = ClosestPlayer.Character
        if not char then continue end
        local hrp = char:FindFirstChild("HumanoidRootPart")
        if not hrp then continue end
        local hum = char:FindFirstChild("Humanoid")
        if not hum then continue end
        
        local path = PathFindingService:CreatePath(AgentParams)
        
        local success, err = pcall(function()
            path:ComputeAsync(Rig:FindFirstChild("HumanoidRootPart").Position, hrp.Position)
        end)
        
        if success then
            if path.Status == Enum.PathStatus.Success then
                for _, waypoint in pairs(path:GetWaypoints()) do
                    
                    RigHum:MoveTo(waypoint.Position)
                    
                    if waypoint.Action == Enum.PathWaypointAction.Jump then
                        RigHum.Jump = true
                    end
                    
                    RigHum.MoveToFinished:Wait()
                end
            end
        else
            warn("Path not calculated:" .. err)
        end
    end
end)
#
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local rs = game:GetService("ReplicatedStorage")

local Rig = script.Parent
local RigHum = Rig:FindFirstChild("Humanoid")

local IdleAnim = rs.Animations.MacheteIdle
local ChaseAnim = rs.Animations.MacheteChase
local HitAnim = rs.Animations.MacheteHit
local IdleTrack = RigHum:FindFirstChild("Animator"):LoadAnimation(IdleAnim)
local ChaseTrack = RigHum:FindFirstChild("Animator"):LoadAnimation(ChaseAnim)
local HitTrack = RigHum:FindFirstChild("Animator"):LoadAnimation(HitAnim)

HitTrack.Looped = false
HitTrack.Priority = Enum.AnimationPriority.Action2
ChaseTrack.Priority = Enum.AnimationPriority.Action

Rig:FindFirstChild("HumanoidRootPart"):SetNetworkOwner(nil)

--Settings
RigHum.WalkSpeed = 30
local MaxDistance = 80
local CD = 2.5
local AgentParams = {AgentCanJump = true, AgentRadius = 1.5}
--Settings
#

^ these go at the top

gray minnow
#

@ocean comet im sorry but i couldnt be bothered to read any of the scripts, but im gonna make an assumption.

You should reduce the wait time if youre using task.wait, or maybe check if the player moved before the pathfind has finished.

also i cant see many obstacles so just use MoveTo() really

ocean comet
#

Tried adjusting wait, doesnt solve the underlying issue that the whole path is finished

winged kindle
#

@ocean comet

#

What i did was raycast, if wall then pathfind else no wall then just moveto

#

its way better

#

no need unnecessary pathfinding when no walls.

ocean comet
#

oh awesome, thx

ocean comet
#

Pathfinding problem (solved)

near wolf