Pathfinding NPC Getting stuck

So I have an NPC that uses pathfinding to get around objects. Now this is the problem: When I test the game using Run, the NPC is perfect and never gets stuck.
However, If I play test the game using the player, the NPC gets stuck on stuff.


script:

local pathfindingService = game:GetService("PathfindingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local foundtarget = nil
local targetpos = nil
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local body = script.Parent:FindFirstChild("Torso")
local stunned = script.Parent.Stunned
local destination = game.Workspace.CampingPath:GetChildren()



local Cooldown = 1
local HitDebounce = false
local MaxDistance = 5

local SwingSounds = Char.AttackZone.SwingSounds:GetChildren()
local HitSounds = Char.AttackZone.HitSounds:GetChildren()









local IdleAnim = humanoid.Animator:LoadAnimation(script.Parent.Idle)
local WalkAnim = humanoid.Animator:LoadAnimation(script.Parent.Walk)
local RunAnim = humanoid.Animator:LoadAnimation(script.Parent.Run)
local Attack1Anim = humanoid.Animator:LoadAnimation(script.Parent.Attack)
local Attack2Anim = humanoid.Animator:LoadAnimation(script.Parent.Attack2)

Attack1Anim.Stopped:Connect(function()
	newHitbox:HitStop()
end)
Attack2Anim.Stopped:Connect(function()
	newHitbox:HitStop()
end)

local blockedConnection


humanoidrootpart:SetNetworkOwner(nil)
local points = {}
for _,part in ipairs(destination) do
	table.insert(points,part)
	task.wait()
end

foundtarget = nil

function walkRandomly()
	foundtarget = nil
	local point = points[math.random(1,#points)]
	local target = FindTarget()
	local path = game:GetService("PathfindingService"):CreatePath({
		AgentRadius = 2,
		AgentHeight = 5,
		AgentCanJump = true
	})
	path:ComputeAsync(humanoidrootpart.Position, point.Position)
	local waypoints = path:GetWaypoints()
	for _, waypoint in ipairs(waypoints) do
		if foundtarget == nil then
			print("DEBUG: WALKING RANDOMLY TO WAYPOINT")
		humanoid:MoveTo(waypoint.Position)
		path.Blocked:Connect(function()
			print("DEBUG: PATH BLOCKED (WALKRANDOMLY)")
			main()
		end)
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		end
		if foundtarget == 1 then path:Destroy() break end
		humanoid.MoveToFinished:Wait()
		FindTarget()
	end
end
--[[
local path = game:GetService("PathfindingService"):CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = false
})
--]]

function main()
	local target = FindTarget()
	if target and foundtarget == 1 then
		print(target)
		local Player = game.Players:GetPlayerFromCharacter(target.Parent)
		humanoid.WalkSpeed = 18
		findPath(target)

	else
		foundtarget = nil
		humanoid.WalkSpeed = 7
		walkRandomly()
	end
end

humanoid.Running:Connect(function(speed)
	if speed > 17 then
		IdleAnim:Stop()
		WalkAnim:Stop()
		RunAnim:Play()
	elseif speed > 4 and speed < 17 then
		IdleAnim:Stop()
		WalkAnim:Play()
		RunAnim:Stop()
	elseif speed <= 4 then
		IdleAnim:Play()
		WalkAnim:Stop()
		RunAnim:Stop()
	end

end)

while RunService.Heartbeat:Wait() do
	main() 
end

some code removed due to being irrelevant at this time

1 Like

This is just a caveat of using PathfindingService unfortunately. From my experience, there isn’t a quick fix for this. (if you’re referring to the NPC clipping around corners, but is able to continue on its path)

A potential fix could be using parts that are collidable to modify the NavMesh, making NPCs avoid those parts, but using collision groups so they don’t actually interfere with the NPC. You could make these parts fairly skinny just enough so they force the NPC away from corners a bit more.

4 Likes

Unfortunately, pathfinding service isn’t really reliable, I’ve seen a lot of people make their own pathfinding systems from scratch because of that, and I definitely will make one as well when I have enough experience.

What you can use to prevent NPCs from getting stuck is, make giant cubes that take all the small objects or anywhere they can get stuck inside of them, because NPC can go around the giant cube easier than a small obstacle that might not even be detected, and then you remove those giant cubes with localscripts that are in playerscripts.

You can even make roads and paths they can take or they cannot take like this, just make walls and enclose it to an area, the NPC can’t leave that area but players can enter and leave because it doesn’t exist on client side… Just be careful that you don’t have an anti-exploit that’ll detect it as exploiting

Kinda sucks that this is the solution. Roblox should really update their pathfinding.
On the side,

Do you know any reliable pathfinding systems to use instead of robloxs?

1 Like

Unfortunately, I know one that is still being made but Idk if they’ll open it to public anyways, your best bet is finding AI/NPC models off the internet (That you are sure doesn’t have any viruses) and see if they have custom pathfinding.