AI Pathfinding Prioritizing paths that put it under the map

While I can’t get a screenshot of the direct issue, I can at least send this example of the AI choosing very bad paths that could potentially put it under the map.
(As seen below, they are close to walking off the map.)
image
Here is the pathfinding code.
Note : pathID and doingPath are related to deciding when to run doPath, and the root is the HumanoidRootPart.

local function doPath(goal)
	pathID = pathID + 1;
	doingPath = true;
	local functionsID = pathID; --currently running this path
	local path = pathfind:CreatePath({
		AgentRadius = 2;
		AgentCanJump = false;
		WaypointSpacing = .5;
	});
	local succ,fail = pcall(function()
		path:ComputeAsync(root.Position, goal.Position);
	end);
	
	if succ then
		local currentWaypoint = 1;
		local allWaypoints = path:GetWaypoints();
		blockCon = path.Blocked:Connect(function(bwi) --blockage
			if bwi >= currentWaypoint then
				blockCon:Disconnect();
				doPath();
			end;
		end);
		
		local function waitMoveTo(goal)
			if pathID == functionsID then --stil walking the path
				if (goal.Position - root.Position).magnitude <= 5 then
					return "Success";
				else
					wait();
					waitMoveTo(goal);
				end;
			else
				return "No Longer Pathfinding"; --id has changed
			end;
		end;
		
		for _,v in pairs(allWaypoints) do
			if pathID == functionsID then --still being run
				humanoid:MoveTo(v.Position);
				if walk.IsPlaying == false then
					walk:Play();
				end;
				local waitResults = waitMoveTo(v);
				if waitResults ~= "Success" then --All other events should end pathfinding
					break;
				end;
			else
				return;
			end;
		end;
		
		--All operations complete
		doingPath = false;
	end;
end;

Do you have any errors? Also, try printing waitResults

local waitResults = waitMoveTo(v);

print(waitResults)

if waitResults ~= "Success" then --All other events should end pathfinding
	break;
end;
1 Like

Try putting an invisible can collide wall there, overlapping the edge of the part with studs a bit but have it in a collision group with the AI characters (So players can walk/fall through it), that way the AI don’t think they can get so close to the edge as they see a part in the way and go around.

1 Like

When I do this, they just run into the wall regardless of collision. Could this potentially be related to the issue?

Yeah, Sound like either Roblox’s pathfinding is broken (I have had it not work a lot lol)
or that the agent radius isn’t set right. But it looks like you have that set up right.

1 Like

Are there any alternatives to the buggy pathfinding ROBLOX provides? Changing the radius up or down doesn’t seem to change much.

None that I have found yet. It’s quite annoying. I have a current issue with pathfinding service too. The best advice I can give is to keep trying new radius and settings and hope it works out. Other than that you can try looking around for alternatives or code one yourself (Which is pretty hard lol)

1 Like

Could you try what I suggested? I’ve never run into an issue like this.

2 Likes

Can you send how exactly you’re calling doPath? Something I realized when looking at your code is that it never prints any error it encounters with the ComputeAsync pcall, a massive oversight, since you might be passing the wrong arguments into doPath.

2 Likes

Here is a grand snip from my while loop. (Mobs target towers or the player, i honestly think something here is causing the bug)

while wait() do
	if script then else
		return;
	end;
	if script.Parent then else
		return;
	end;
	local health = tween:Create(healthbar.Frame.Frame, TweenInfo.new(.5), {Size = UDim2.new(stat.Health.Value/stat.Max.Value, 0, 1, 0)});
	health:Play();
	if stat.Health.Value <= 0 then
		script.Parent:Destroy();
	end;
	local allmobs = troops:GetChildren();
	--Gain closest mob
	local subj = nil;
	local part = nil;
	for _,v in pairs(allmobs) do
		local theMobsPart = v.HumanoidRootPart;
		if subj == nil then
			subj = v;
			part = subj.HumanoidRootPart;
		else
			if (theMobsPart.Position-root.Position).magnitude < (part.Position-root.Position).magnitude then
				subj = v;
				part = theMobsPart;
			end;
		end;

	end;

	if subj ~= nil then
		if (part.Position-root.Position).magnitude > range then
			if doingPath == false then
				doPath(part);
			end;
		else
			inRange();
			dmg(subj);
		end;
	else
		if root.Velocity == Vector3.new(0,0,0) then
			if stand.IsPlaying == false then
				stand:Play();
			end;
			walk:Stop();
		else
			if walk.IsPlaying == false then
				walk:Play();
			end;
			stand:Stop();
		end;

	end;


	if subj == nil then --Do Tower Instead
		local towers = map.PlayersTowers;
		for _,v in pairs(towers:GetChildren()) do --acquire closest tower
			local theMobsPart = v.main;
			if subj == nil then
				subj = v;
				part = subj.main;
			else
				if (theMobsPart.Position-root.Position).magnitude < (part.Position-root.Position).magnitude then
					subj = v;
					part = theMobsPart;
				end;
			end;
		end;

		if (part.Position-root.Position).magnitude > range*1.35 then
			if doingPath == false then
				doPath(part);
			end;
		else
			inRange();
			dmg(subj);
		end;
	end;
end;

In the future when PathfindingModifiers get released, you could probably create some transparent blocks positioned near the edges of the map and make it so they have very high costs, perhaps that’ll affect how close your NPCs get to it? And since it’s a non-collidable part, you could even make it larger than the actual dropoff to correct it more precisely.


I even made it visible. They seem to just completely ignore the existence of barriers.

image

Looks something like this when tested. I’m pretty lost on how to translate what this all means – guessing it has to do with obstacles.

They are abiding by it, but pathfinding service makes the shortest path to the destination. Not the most user-friendly or pretty path. If you visualize the waypoints that pathfinding service is creating, you’ll notice that they’ll be right up against the wall because it’s the shortest path.

I would say definitely take a look at pathfinding modifiers. Unfortunately they are still in beta so you can’t actually use them for production yet but you should consider testing them out in studio.

In the meantime, mess with the AgentRadius some more to see if you can get them to stop bumping into the walls.

1 Like

The modifiers look perfect, pretty sad that they can’t be used. I still keep changing the Radius to no avail, so I may just be screwed until this new update releases.