Pathfinding not working

Hi, I created a script to make an NPC chase after the nearest player to them endlessly. However, the NPC will run into walls and will not avoid obstacles to get to the player. I linked my code below for reference. Any help or ideas on why this is happening would be greatly appreciated.

-- Get the NPC and its humanoid
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
-- Put this script inside the NPC model in Roblox Studio

local humanoid = script.Parent:WaitForChild("Humanoid")
local pathfindingService = game:GetService("PathfindingService")
local players = game:GetService("Players")
local npcSpeed = 16 -- Adjust the speed of the NPC as needed

local function findClosestPlayer()
	local closestPlayer = nil
	local closestDistance = math.huge

	for _, player in ipairs(players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			local distance = (player.Character.HumanoidRootPart.Position - humanoid.RootPart.Position).Magnitude
			if distance < closestDistance then
				closestPlayer = player
				closestDistance = distance
			end
		end
	end

	return closestPlayer
end

local function pathToPlayer(player)
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(humanoid.RootPart.Position, player.Character.HumanoidRootPart.Position)
	return path
end

while true do
	local closestPlayer = findClosestPlayer()

	if closestPlayer then
		local path = pathToPlayer(closestPlayer)
		path:ComputeAsync(humanoid.RootPart.Position, closestPlayer.Character.HumanoidRootPart.Position)
		local waypoints = path:GetWaypoints()

		for _, waypoint in ipairs(waypoints) do
			humanoid:MoveTo(waypoint.Position)
		end
	end

	wait(0.04)
end

Hello Cdweave,

Thank you for reaching out! I’ve created a template for you to address the issue with your NPC’s obstacle avoidance. You can find the template below:


PathFinding by cgi_eric.rbxl (124.5 KB)


-- Get the NPC and its humanoid
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")

local pathfindingService = game:GetService("PathfindingService")
local players = game:GetService("Players")
local npcSpeed = 16 -- Adjust the speed of the NPC as needed

local function findClosestPlayer()
	local closestPlayer = nil
	local closestDistance = math.huge

	for _, player in ipairs(players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			local distance = (player.Character.HumanoidRootPart.Position - humanoid.RootPart.Position).Magnitude
			if distance < closestDistance then
				closestPlayer = player
				closestDistance = distance
			end
		end
	end

	return closestPlayer
end

local function pathToPlayer(player)
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(humanoid.RootPart.Position, player.Character.HumanoidRootPart.Position)
	return path
end

while true do
	local closestPlayer = findClosestPlayer()

	if closestPlayer then
		local path = pathToPlayer(closestPlayer)
		local waypoints = path:GetWaypoints()

		for i, waypoint in ipairs(waypoints) do
			local nextWaypoint = waypoints[i + 1]
			if nextWaypoint then
				humanoid:MoveTo(nextWaypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	end

	wait(0.04)
end

PathFinding by cgi_eric.rbxl (124.5 KB)

Please replace your existing code with the template provided and test it in your game environment. This updated version should help your NPC avoid obstacles and chase after the nearest player smoothly.

Please also consider agentParameters. These values affect how large a ga[ needs to be (ie with doorways) for the NPC to fit through.