Path finding errors, not able to create path?

Hello, I am working on path finding for a zombie AI and it seems that when the zombie is spawned in the game and you are behind a cancollided wall he will create a path. He will find you in the players and label you a target, but he can’t create a path to you, but no errors pop up. It seems as if it is just a bug that I don’t know how to fix. I was wondering if there was a way to fix this, or to check to see if a path was created, and if it wasn’t created maybe he can be deleted from the workspace and respawned somewhere else.
I am not quite sure how to check if a path was created. Could anyone give me some pointers?
Below I have posted a picture that I will explain.

Here is my code:


function findPath(target)
	showImage()
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position,target.Position)
	local waypoints = path:GetWaypoints()
	local success = path.Status == Enum.PathStatus.Success
	if success then
		for _, waypoint in ipairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(waypoint.Position)
			local timeOut = myHuman.MoveToFinished:Wait(1)
			if not timeOut then
				myHuman.Jump = true
				print("Path too long!")
				findPath(target)
				break
			end
			if checkSight(target) then
				repeat
					--print("Moving directly to the target")
					myHuman:MoveTo(target.Position)
					attack(target)
					wait(0.1)
					if target == nil then
						break
					elseif target.Parent == nil then
						break
					end
				until checkSight(target) == false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
				break
			end
			if (myRoot.Position - waypoints[1].Position).magnitude > 20 then
				print("Target has moved, generating new path")
				findPath(target)
				break
			end
		end
	end
end

This picture is taken during a test play and I am in the server view.
In this picture on the left you can see a zombie that has found a path because it is playing the moving animation. On the right you can see the same type of zombies that could not find a path and are not moving. My character is behind that black wall on the right.

Are you running a while wait() do on your findPath() function or a slower loop? If a path isn’t found, it should be able to just loop again until a path is found, or at least the nearest path.

You can tell if a path is created by print(waypoint)

if success then
      pathfind
else
        warn("Can't find a path")
        local path = game:GetService("PathfindingService"):CreatePath(pathargs)
		local x = hrp.Position.X + math.random(-5,5)
		local z = hrp.Position.Z + math.random(-5,5)
		path:ComputeAsync(hrp.Position,Vector3.new(x,0,z))
       -- do another in pairs loop to pathfind a new path, 
end

If you’re running a while wait() do on your findPath() function then pathfinding in the else might not be so necessary, definitely keep the warning there for debugging.

Also make path arguments for a smoother ai.

local pathargs = {
	["AgentHeight"] = 5,
	["AgentRadius"] = 2,
	["AgentCanJump"] = true
}

Not to relevant to what your issue is but for the future, do this just incase. If he is cutting corners too tightly, make the radius a bit bigger. For the nocollide issue, There’s a devforum post stating that collisiongroups aren’t recognized by the pathfinding ai, so make sure you just nocollide it.

1 Like

Your “Not to relevant to what your issue is but for the future” actually was my issue! Thank you so much haha, I have “Zombie Doors” that are cancollide true to players but not zombies. There was no problem with my zombie AI at all. It was that simple little thing that can be easily looked over.

2 Likes