Pathfinding npc is not really finding any path?

Hello! I don’t like asking for help but since I couldn’t find any answer to this, I had to ask it myself.

Here’s my code:

local npc = script.Parent
local hum = script.Parent:WaitForChild("Humanoid")
local pathService = game:GetService("PathfindingService")
npc.PrimaryPart:SetNetworkOwner(nil)



local maxDistanceVariable = math.huge

local height = 19 
local thickness = 9 
local canJump = false 
local speed = 60 





local paramaters = {
	["AgentHeight"] = height,
	["AgentRadius"] = thickness,
	["AgentCanJump"] = canJump 
}

hum.WalkSpeed = speed



local function getPlayer()
	local plrs = game:GetService("Players"):GetChildren()
	local maxDis = maxDistanceVariable
	local nearestPlr 
	
	for i, plr in pairs(plrs) do
		if plr.Character ~= nil then
			local target = plr.Character
			local distance = (npc.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDis then
				nearestPlr = target
				maxDis = distance
			end
		end
	end
	
	return nearestPlr
end


local function getPath(destination)
	local Path = pathService:CreatePath(paramaters)
	Path:ComputeAsync(npc.HumanoidRootPart.Position, destination)
	return Path
end

local function moveTo(character)
	local char = character
	local path = getPath(char.HumanoidRootPart.Position)
		
	for i, waypoint in pairs(path:GetWaypoints()) do
		hum:MoveTo(waypoint.Position)
	end
end


while wait() do
	local plrTarget = getPlayer()
	if plrTarget ~= nil then
		moveTo(plrTarget)
	end
end

The point is, based on my understanding, this should allow the npc to figure out a way past walls or obstacles. It does not do that, it’s parameters are correct and everything is working as far as I’m aware. What is the issue here?

1 Like