NPC Pathfinding Broken

Hello Roblox Devs,

I have attempted to make a pathfinding script that will find a player within a certain distance. Unfortunately, it does not work.

What did i do wrong?

Code:

local PositionTorso = script.Parent.Torso.Position
local NPC = script.Parent -- If your script is inside the NPC.

NPC.PrimaryPart:SetNetworkOwner(nil)


local PathfindingService = game:GetService("PathfindingService")
local pathParams = {
	AgentHeight = 5,
	AgentRadius = 3,
	AgentCanJump = true,
}


local function getPath(destination)
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination.Position)

	return path	
end




function findNearestTorso(pos)
		
	local list = game.Workspace.Players:GetChildren()
	local torso = nil
	local dist = 110
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("UpperTorso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	
	return torso
end

local humanoid = script.Parent.Hum

while wait(0.01) do
	
	PositionTorso = script.Parent.Torso.Position
	local target = findNearestTorso(PositionTorso)
	print(target)
	
	local path = getPath(target)
	
	if target ~= nil then
		--script.Parent.Random.Value = false
		if path.Status == Enum.PathStatus.Success then
			for i, waypoint in pairs(path:GetWaypoints()) do
				path.Blocked:Connect(function()
					path:Destroy()
					print("Blocked")
				end)

				if target.Humanoid.Health > 0 then
					lastPos = target.HumanoidRootPart.Position
					break
				else
					if waypoint.Action == Enum.PathWaypointAction.Jump then
						humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
					end

					if lastPos then
						humanoid:MoveTo(lastPos)
						humanoid.MoveToFinished:Wait()
						lastPos = nil
						break
					else
						humanoid:MoveTo(waypoint.Position)
						humanoid.MoveToFinished:Wait()
					end
				end
			end
		else
			return
		end
	else
		--script.Parent.Random.Value = true
	end
end
2 Likes

Can you send me a video? (huiasdufdihyghfsd)

1 Like

robloxapp-20230207-2004126.wmv (1.1 MB)

The NPC Can find a target, the pathfinding code just does not create a path for the NPC.

Try printing for debugging. (eg. path.Blocked:Connect(function() path:Destroy() print("blocked!") end))

I have narrowed it down to here:

if path.Status == Enum.PathStatus.Success then
			print("error")
			for i, waypoint in pairs(path:GetWaypoints()) do
				path.Blocked:Connect(function()
					path:Destroy()
					print("Blocked")
				end)

				if target.Humanoid.Health > 0 then
					print("error1")
					lastPos = target.HumanoidRootPart.Position
					break
				else
					print("error2")
					if waypoint.Action == Enum.PathWaypointAction.Jump then
						print("error3")
						humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
					end

					if lastPos then
						print("error4")
						humanoid:MoveTo(lastPos)
						humanoid.MoveToFinished:Wait()
						lastPos = nil
						break
					else
						print("error5")
						humanoid:MoveTo(waypoint.Position)
						humanoid.MoveToFinished:Wait()
					end
				end
			end
		else
			return
		end

The code will not start the “if path.Status == Enum.PathStatus.Success then” there fore the path is not being successfully made.

Path Function:

local function getPath(destination)
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination)

	return path	
end

PositionTorso = script.Parent.Torso.Position
local target = findNearestTorso(PositionTorso)
1 Like

Try printing the path object and path.Status.

1 Like

It says that the target is = to nil

Find Target:

function findNearestTorso(pos)
		
	local list = game.Workspace.Players:GetChildren()
	local torso = nil
	local dist = 110
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("UpperTorso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	
	return torso
end

Do you want me to replace my whole code? Because that script does not have a way to call some functions.

1 Like

Change temp= temp2:findFirstChild("UpperTorso") to temp= temp2:findFirstChild("HumanoidRootPart") for support with all rig types.

Still says target is nil

ewgg weg wg wegrweh rwe