So i’m currently making an pathfinding npc which goes to the closest waypoint with pathfinding and then removes from the list beacuse it’s got checked (and it won’t target that waypoint until he will check all waypoints on the map), but there’s problem…
If there’s waypoint behind the wall, it will be closest waypoint, but path isn’t actually closest. I tried to make function where script checks how many points npc needs to reach the target, and it works, except for thing it takes really long… I wrote print thing and it works not instantly, every pathfind game makes takes at least 0.1 second and my npc doesn’t going anywhere for that time.
Here’s script:
local function ClosestPoint(list)
for i, waypoint2 in ipairs(list) do
local path2 = PathfindingService:CreatePath(HumanoidProperties)
if LastTarget == nil then
path2:ComputeAsync(NPC.HumanoidRootPart.Position, waypoint2.Position)
else
path2:ComputeAsync(LastTarget.Position, waypoint2.Position)
end
if path2.Status ~= Enum.PathStatus.NoPath then
local WayPoints = path2:GetWaypoints()
print(#WayPoints)
table.insert(waypoints3, #WayPoints)
end
end
for i, waypoint2 in ipairs(waypoints3) do
if BestPointDistance ~= nil then
if BestPointDistance < waypoint2 then
BestPointDistance = waypoint2
BestPoint = waypoint2
end
else
BestPointDistance = waypoint2
BestPoint = waypoint2
end
end
end
How do i fix delay when finding closest path with pathfinding?
what i understand that you have a certain number of waypoints and you are trying to move your npc to the closest way point but the pathfindingservice sees the the nearest way point is not the nearest because it is behind a wall right ?
yeah, i mean i’m not finding the closest waypoint to the npc but closest waypoint to the last waypoint it targetted, but still the same problem. There could be a waypoint in 20 studs from last waypoint, but to get to that waypoint with pathfinding would take for example, 100 studs.
you can get the distance between any object by using (Part1.Position - Part2.Position).Magnitude by this you can make a for loop to loop into every way point compare the distance and get the nearest one with out using path finding service to get the nearest way point
I think you can get the distance between the two waypoints like this but I am on my break so I may be wrong:
local distancetable = {}
for i, wp in waypoints do
local distance = (zombie.Position - wp.Position).Magnitude
table.insert(distancetable, distance)
end
table.sort(distancetable)
this is what i already said, also you do not have to use tables you can store the nearest point in a variable just like this local nearestpoint = nil for i , point in waypoints do if nearestpoint == nil then nearestpoint = point else local distance1 = (zombie.PrimaryPart.Position - point.Position).Magnitude local distance2 = (zombie.PrimaryPart.Position - nearestpoint.Position).Magnitude if distance1 < distance2 then nearestpoint = point end
The problem is that i already made something like that but to make pathfind it takes time so npc stops for a moment. Or maybe i didn’t i’m not sure at this point, the post was made a while ago yk, so i don’t remember…
But now i realised that i could just make calculation before the npc even reached the next waypoint instead of doing it when he does, so he already knows where to go next without taking some time, and i feel stupid now.
Anyways, thanks for trying to help, i hope your post will help somebody else too!