Pathfinding breaks randomly

Its my first time posting on the DevForum so bear with me here.

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to try and find a solution to my problem, or receive some potential help in figuring out why it happens.

  2. What is the issue? Include screenshots / videos if possible!
    The enemies in my game randomly stop following the player. I’ve revised the code many times to try and find a reason why they would act this way but to no avail.

How they’re supposed to act

How they’re acting

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried to “fix” as many things that could cause their AIs to break, but it still happens. I did look nonstop for posts regarding a similar problem as mine but it seems like it’s something that nobody has posted about yet.

One thing I’ve noticed is that this seems to happen randomly. It started happening this Friday after a Roblox update, affecting both the Development and Main branches of my game. I noticed that whatever this issue is, it seems to be somewhat related to pathfinding generation since I use a system where the enemy attacks whoever he’s following; this means the enemy can get the closest player but doesn’t walk up to them.

Another thing that has happened is that the enemies seemingly started walking to random spots on the maps (I don’t have footage of this since I’ve only seen it happen once or twice) and either stayed there or went back to following the closest player.

Here’s the AI code for everyone interested in helping.


--// NPC Variables

local NPC = script.Parent
local hum = NPC.Humanoid
local hrp = NPC.HumanoidRootPart

local CurrentTarget = NPC.CurrentTarget

--// Services

local PFS = game:GetService("PathfindingService")
local RunS = game:GetService("RunService")

--// Other Variables

local Lim = math.huge
local PathPoints = nil

--// Functions

local Raycast = require(game.ServerScriptService.ModuleScripts.CustomRaycast)

function findNearestTorso(pos)
	local list = game.Players:GetPlayers()
	local torso = nil
	local dist = Lim

	for i,who in pairs(list) do
		if who.Character then
			local whochar = who.Character
			if whochar and whochar:FindFirstChildOfClass("Humanoid") and whochar:FindFirstChild("HumanoidRootPart") then
				local whohrp = whochar.HumanoidRootPart
				local whohum = whochar.Humanoid

				if whohum.Health > 0 then
					if (whohrp.Position - pos).Magnitude <= dist then
						torso = whohrp
						dist = (whohrp.Position - pos).magnitude
					end
				end
			end
		end
	end
	return torso
end

function UpdateCT(who)
	if who ~= CurrentTarget.Value then
		CurrentTarget.Value = who
	end
end

--// Path Setup

local PathSettings = {
	AgentRadius = 3 * NPC:GetScale(),
	AgentHeight = 6 * NPC:GetScale(),
	AgentCanJump = true,
	WaypointSpacing = math.sqrt(hum.WalkSpeed),
}

--// Run

while task.wait(0.1) do
	local whohrp = findNearestTorso(hrp.Position)

	if whohrp and whohrp.Parent and whohrp.Parent:FindFirstChildOfClass("Humanoid") then

		local who = whohrp.Parent
		local whohum : Humanoid = who.Humanoid

		UpdateCT(who)
		
		local PathSetup : Path = PFS:CreatePath(PathSettings)
		
		local res1 = pcall(function()
			PathSetup:ComputeAsync(hrp.Position, whohrp.Position)
		end)
		
		if res1 then
			local res2 = pcall(function()
				PathPoints = PathSetup:GetWaypoints()
			end)

			if res2 then
				if PathPoints and PathSetup.Status == Enum.PathStatus.Success and PathPoints[math.min(3, #PathPoints)] then
					if PathPoints[math.min(3, #PathPoints)].Action == Enum.PathWaypointAction.Jump then
						hum.Jump = true
					end

					hum:MoveTo(PathPoints[math.min(3, #PathPoints)].Position)
				end
			end
		end
	else
		UpdateCT(nil)
	end
end

Note: this code used to be cleaner but I just wanted to try everything possible to fix this.

Update: I made the path waypoints visible when the enemy starts following you. When the enemies stop walking, no path waypoints are generated, meaning the problem is steaming from the Pathfinding system.

1 Like

The “true” warn is the result of pcalling the :GetWaypoints() function, meaning its not causing an error, but instead, its returning no path. The blank warn is the path itself.

[...]
local PathSetup : Path = PFS:CreatePath(PathSettings)
		
		local res1 = pcall(function()
			PathSetup:ComputeAsync(hrp.Position, whohrp.Position)
		end)
		
		if res1 then
			local res2 = pcall(function()
				PathPoints = PathSetup:GetWaypoints()
			end)
			
			warn(res2)

			if res2 then
				warn(table.unpack(PathPoints))
				if PathPoints and PathSetup.Status == Enum.PathStatus.Success and PathPoints[math.min(3, #PathPoints)] then
					if PathPoints[math.min(3, #PathPoints)].Action == Enum.PathWaypointAction.Jump then
						hum.Jump = true
					end

					hum:MoveTo(PathPoints[math.min(3, #PathPoints)].Position)
					
					if LastPathModel then
						LastPathModel:Destroy()
					end
					LastPathModel = VisualizePath(PathPoints)
				end
			end
		end
[...]

Hi, ROBLOX Pathfinding is currently broken on specific maps(which might be the map size) try to use the zombie on the default baseplate with an instanced part for wall, then show how it works

I genuinely would do that but not only does this issue happen randomly, but if the issue is truly the map size, then I can’t fix it since my game requires large maps for the movement to be useable.