# Buggy Pathfinding

Hey, to anyone reading, I’ll cut right to the point. I’m trying to have an efficient and at least more than semi-functional pathfinding for each and every “zombie” shown in the video.

Watch this video before reading on:

Briefly explaining how they path find to their target: I first just check if their target is in a straight line of sight from the zombie by creating a Ray. If so, I’ll simply have them move towards the target, otherwise it’ll move onto finding a more complex route to the target. If you rewatch the clip above, you’ll see that when I cut around corners or edges where zombies can no longer see me, they’ll calculate a new route with a slight problem. The issue is the fact they’ll bug back and this may repeat itself a lot several times with in a second as well making it almost impossible for zombies to ever even reach their target if they’re cutting corners excessively.

The code here shows how if the target has moved from the position of the final waypoint, then the zombie will then calculate a new path to the target. This is when the jittery back movement kicks in.

``````if (waypoints[#waypoints].Position - TargetHRP.Position).Magnitude > 15 then
zombie.Humanoid:MoveTo(zombie.HumanoidRootPart.Position)
break
end
``````

I’m not sure if this is a cause of the zombies insane speed or not, however I can’t really change the speed as it is very necessary. So what solutions are there?

This post may feel rushed and all over the place, but if your interest is peaked, I’ll be more than willing to elaborate more to better get an explanation. Any help appreciated.

2 Likes

Still haven’t figured it out lul.

The code you provided is moving the zombie to it’s own position if I understood that right, so until the next loop of code runs the zombie will be in it’s previous position for a split second.

Also, is this definitely the cause of the problem? (did you comment it out and test).

Yes, I printed out many steps with in the code, and this is when it typically occurs. I need to figure out a way to to keep them from bugging out like that lol.

Try commenting/removing that part of the code out and see if it still happens (don’t know if you tried that already from your reply)

The reason they move back to their original position is so that then a new path can be calculated, however it doesn’t matter if I do or do not have that line of code, when a new path is computed, they still bug back.

I mean I’ve tried several upon several things, even looking at the devforums, but I’ve come to no solution and that’s why I’m posting it now

Alright, good to know. If that’s not the root cause of the problem are you able to provide some more of your code or even provide the place file?

I could provide more code. I can show the part that actually computes the path (after checking if the player is in the ray)

Why does a zombie go to itself?

Well it isn’t extremely necessary but the point is for it to stop where it’s at, therefore it goes to itself, and then it computes a new path. Even without this, they still bug back anyways.

it means that you have a script for searching the path and following it incorrectly, first make it without rays, and then, if everything is fine, add a ray or post all your code here

You guys think if I gave a sample of my code it’ll help elaborate?

Well, how else can we figure it out?

you have a little incomprehensible code, so make the code based on this example and then expand the functionality further

``````

local reached
MoveToFinishedCon = npcHum.MoveToFinished:Connect(function(Reached)

if Reached then
reached = Reached
end
end)
while npcHum.Health > 0 do
local success, errorMessage = pcall(function()
path:ComputeAsync(npcHRP.Position, closestChrHRP.Position)
end)

if success and closestChrHRP and path.Status == Enum.PathStatus.Success then

local waypoints = path:GetWaypoints()

for i=2, #waypoints do

if waypoints[i].Action == Enum.PathWaypointAction.Jump then
npcHum:ChangeState(Enum.HumanoidStateType.Jumping)
end

npcHum:MoveTo(waypoints[i].Position)

local cnt = 0
reached = false
repeat
cnt+=1
--print(cnt)
until reached or cnt == 60 --
end
end
end
``````

Also, why would I be connecting a function MoveToFinished if it doesn’t have a waypoint to finish at to start with anyways…?

until the player reaches the next point, this loop will keep the process moving to the next point

``````repeat
cnt+=1
--print(cnt)
until reached or cnt == 60
``````

``````while npcHum.Health > 0 do