Pathfinding service is stupid

Hey guys it’s agKing here and I have a problem with the pathfinding service so basically I’m making a brainrot game in which I want NPCs to go to my plot and to the TeleportPart but then some NPCs run into obstacles along the way such as Wall, Trees etc. and it’s very annoying, I’ve tried changing some parameters of the agent and it doesn’t seem to change anything at all. I’ve did some research and some people says that PathfindingService seems to struggle with meshparts as our game consists of lots of Meshparts done in blender.

Here’s the video showing the NPC stupid behavior

Here’s the code of the agent. I’ve tried following the code of a blog for when the npc struggle to go to its destination to recalculate the path but that doesn’t seem to work either

function CharacterHandler:MoveThrough(destinations)
	local hum = script.Parent:FindFirstChild("Humanoid")
	local root = script.Parent:FindFirstChild("HumanoidRootPart")
	if not hum or not root then return end

	script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

	local AGENT_PARAMS = {
		AgentRadius = 5,
		AgentHeight = 5,
		AgentCanJump = true,
		AgentJumpHeight = 10,
		AgentMaxSlope = 45,
		WaypointSpacing = 6
	}

	task.spawn(function()
		for _, destination in ipairs(destinations) do
			local function navigateTo(dest)
				local path = PathfindingService:CreatePath(AGENT_PARAMS)
				local success, err = pcall(function()
					path:ComputeAsync(root.Position, dest.Position)
				end)

				if not success or path.Status ~= Enum.PathStatus.Success then
					warn("Path failed to "..dest.Name..":", path.Status)
					return
				end

				-- Connexion pour gérer les blocages
				local blockedConn
				blockedConn = path.Blocked:Connect(function()
					blockedConn:Disconnect()
					navigateTo(dest) -- recalcul si bloqué
				end)

				-- Suivi du chemin
				for _, wp in ipairs(path:GetWaypoints()) do
					if wp.Action == Enum.PathWaypointAction.Jump then
						hum:ChangeState(Enum.HumanoidStateType.Jumping)
					end

					hum:MoveTo(wp.Position)
					hum.MoveToFinished:Wait()
				end

				blockedConn:Disconnect()
			end

			navigateTo(destination)
		end
	end)
end
3 Likes

Try to put invisible parts in the walls

1 Like

Hey there. Since your research yielded the conclusion that mesh parts cause issues in pathfinding, I would recommend you create a part with the same size and position as the mesh part. You can then set the transparency to 1 and have it as sort of a “hitbox.” This way, pathfinding won’t be struggling to calculate with the mesh part—it will only need to calculate with the hitboxes.

1 Like

Yeah I’ll try to put normal part to replace walls

1 Like

Actually it doesn’t change anything whether I put a BasePart or a MeshPart I tried putting wall and it didn’t change anything at all

2 Likes

canjump = false?

2 Likes

Change AgentCanJump to false, it looks as if the humanoids are trying to jump over but they are not able to.

1 Like

I’ll try this then let’s see if that does

local AGENT_PARAMS = {
		AgentRadius = 5,
		AgentHeight = 5,
		AgentCanJump = true,
		AgentJumpHeight = 10,
		AgentMaxSlope = 45,
		WaypointSpacing = 6
	}

Nope it still does the same error

I wonder if I would have to find my own pathfinding algorithm but it’s going to take a massive time investment from me and I’m not sure if I’m ready to invest this time and energy into making my own solution. I’ve looked up in the internet and I’ve found the A* algorithm

Try adding AgentCanClimb = false to the params

If your NPC should be able to jump, but don’t, check the value of the Humanoid.JumpPower, make sure it’s higher than 0.

Also to make it jump, instead of relying on the Humanoid’s state, use the property directly Humanoid.Jump = true

It could also be a problem that the WaypointSpacing is too big and can’t register that there is a wall and that it should jump. Try lowering the value

Let’s try adding this parameter then

What value would you give to the spacing ?

local AGENT_PARAMS = {
		AgentRadius = 5,
		AgentHeight = 5,
		AgentCanJump = true,
		AgentCanClimb = false,
		AgentJumpHeight = 10,
		AgentMaxSlope = 45,
		WaypointSpacing = 6
	}

Yeah my AI are very dumb I added the param AgentCanClimb to false

Given the walls thickness, I’ll probably say somewhere along 1 (you can always tweak it later)

the AgentCanClimb parameters applies for Truss parts or ladder-like models where you have a climbing animation.

I changed it to 1 still problem remains I tried one time adding a Cost parameter table in which I would add the part that my humanoid I want to avoid but actually I’m not sure to understand the Costs parameters anyway

Do you have a file you can provide to reproduce this issue?

No I don’t for the moment I would need to create an empty workplace with obstacles etc.