#Pathfinding service question

1 messages · Page 1 of 1 (latest)

limber axle
#

Should I use MoveToFinished? for pathfinding? It seems like the only way to follow paths correctly, but is very unresponsive.

visual beacon
#

you shouldn't rely on MoveToFinished for pathfinding. That works, yes, but it's very slow because the script waits for the character to reach the point before continuing. It would be best to move the character with a loop that checks its distance to the next waypoint. This way, you can avoid potential jams

limber axle
#

@visual beacon

#

ye

#

i need to learn distance check

#

can you help?

visual beacon
#

sure

#

a distance check means measuring how far two points are from each other

you can find distance like this

local part1 = workspace.PartA
local part2 = workspace.PartB

local distance = (part1.Position - part2.Position).Magnitude
print(distance)

(part1.Position - part2.Position) gives a vector pointing from one to the other

.Magnitude gives the distance of the vector

#

and there are a example with a npc

limber axle
#

I know

#

What should i do with it tho?

visual beacon
#

you can do this using distance checker + moveFinished if you implement it well

#

give me a second I'll write it down

limber axle
#

Heres my code rn

#
    while flag do
        wait(0.1)
        local target = findTarget(FaceStealer)
        local path = ps:CreatePath({
            AgentRadius = 2,
            AgentHeight = 5,
            AgentCanJump = true,
            AgentJumpHeight = 3,
            AgentMaxSlope = 45,
        })

        if target and target.Character then
            print("Chasing")
            path:ComputeAsync(FaceStealer.HumanoidRootPart.Position, target.Character.HumanoidRootPart.Position + Vector3.new(0, 2, 0))
        else
            local point = randomLocation(FaceStealer)
            print("Wandering")
            path:ComputeAsync(FaceStealer.HumanoidRootPart.Position, point + Vector3.new(0, 2, 0))
        end
        print(path.Status)
        if path.Status == Enum.PathStatus.Success then
    
            local waypoints = path:GetWaypoints()
            for _, path in pairs(waypoints) do
                drawWaypoint(path.Position)
                if path.Action == Enum.PathWaypointAction.Jump then
                    FaceStealer.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
                end
                humanoid:MoveTo(path.Position)
            
            end
        end
    end
end) ```
#

I don't want movetofinsihed

visual beacon
#
task.spawn(function()
    while flag do
        wait(0.1)
        local target = findTarget(FaceStealer)
        local path = ps:CreatePath({
            AgentRadius = 2,
            AgentHeight = 5,
            AgentCanJump = true,
            AgentJumpHeight = 3,
            AgentMaxSlope = 45,
        })

        if target and target.Character then
            print("Chasing")
            path:ComputeAsync(FaceStealer.HumanoidRootPart.Position, target.Character.HumanoidRootPart.Position + Vector3.new(0, 2, 0))
        else
            local point = randomLocation(FaceStealer)
            print("Wandering")
            path:ComputeAsync(FaceStealer.HumanoidRootPart.Position, point + Vector3.new(0, 2, 0))
        end

        print(path.Status)
        if path.Status == Enum.PathStatus.Success then
            local waypoints = path:GetWaypoints()
            for _, waypoint in pairs(waypoints) do
                drawWaypoint(waypoint.Position)

                if waypoint.Action == Enum.PathWaypointAction.Jump then
                    FaceStealer.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
                end

                humanoid:MoveTo(waypoint.Position)

                local startTime = tick()
                local timeout = 5
                while (FaceStealer.HumanoidRootPart.Position - waypoint.Position).Magnitude > 5 and tick() - startTime < timeout do
                    wait(0.1)
                end
            end
        end
    end
end)

I added a distance-checking loop that waits for the character to reach each waypoint before moving to the next one. The loop uses .Magnitude to calculate the distance between the character and waypoint if it's greater than 5 studs, it keeps waiting. It also has a 5-second timeout to prevent getting stuck

visual beacon
limber axle
#

@visual beacon Dude

#

Ty

#

Lemme look at it

#

@visual beacon

#

local startTime = tick()
local timeout = 5
while (FaceStealer.HumanoidRootPart.Position - waypoint.Position).Magnitude > 5 and tick() - startTime < timeout do
wait(0.1)
end

#

Wait

#

why wait 0.1?

visual beacon
# limber axle why wait 0.1?

If you use a shorter wait like wait(0.01), it checks more often but uses more CPU. 0.1 is a good balance frequent enough to feel responsive, but not so fast it wastes resources

split dawn
#

Hmm

delicate fox
delicate fox
delicate fox
delicate fox
#

what they don't understand is the fact that MoveToFinished:Wait() is a newb trap that they fell into and didn't understand how to get out of it so they have resorted to these big dum solutions instead of simply reading the documentation

delicate fox
#

follow the docs and connect to it properly as shown in the examples, that way you can easily interrupt the movement and adjust the path it is following without having to wait for it to reach the next waypoint (you can't do this with MoveToFinished:Wait() )

limber axle
#

@delicate fox

#

Wait

limber axle
#

if i don’t use a task.spawn then what should i use?

#

nvm

#

i got what u saying

limber axle
delicate fox
#

i said connect to movetofinished as shown in the docs. do not use MoveToFinished**:Wait()** because it is a newb trap

#

the :Wait() is what traps you

#

not the event itself