#Pathfinding service question
1 messages · Page 1 of 1 (latest)
hello
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
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
you can do this using distance checker + moveFinished if you implement it well
give me a second I'll write it down
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
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 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?
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
Hmm
this is absolutely horrible advice so awfully bad that you should feel bad for saying it.
this doesn't work and immediately moves towards the final waypoint
and this is just plain bad
Yes, you should absolutely use MoveToFinished when attempting any kind of pathfinding. Even if you don't use pathfindingservice, if you want the npc to follow a series of waypoints you should still use MoveToFinished
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
it's "slow" and "unresponsive" because you're using it wrong. do not use MoveToFinished:Wait() because that will always jam your movement code
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() )
Wait why you said to use it here
Then say don't use it
you didn't read what i said properly
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