Moving an NPC to closest player (need help with the pathfinding part)

I’m currently trying to create an AI, and I have used a findNearestTorso function to find closest player, and when I try to have the AI pathfind to said player, nothing happens. I have searched online how to do pathfinding and I am not sure what I am doing wrong, as there are no errors.

The code:

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()

local AI = script.Parent
local humanoid = AI.Humanoid

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 15
	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("HumanoidRootPart")
			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


while true do
	wait(1)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		--script.Parent:MoveTo(target.Position, target) -- this works, but the pathfinding doesn't

		--compute the path
		path:ComputeAsync(AI.HumanoidRootPart.Position, target.Position)
	end
end

Hello,

I made something similar not too long ago and this is the function I used for getting the closest torso:

function getNearestTorso(position)
    local nearestCharacter, nearestDistance

    for _, player in pairs(playersService:GetPlayers()) do
        local character = player.Character

        if character and character:WaitForChild("Humanoid").Health > 0 then
            local distance = 0
            local playerHumanoidRootPart = character:WaitForChild("HumanoidRootPart")

            local path = pathfindingService:FindPathAsync(position, playerHumanoidRootPart.Position)

            if path.Status == Enum.PathStatus.Success then
                local previousPosition

                for index, waypoint in pairs(path:GetWaypoints()) do
                    if index > 1 then
                        distance += (previousPosition - waypoint.Position).Magnitude
                    end

                    previousPosition = waypoint.Position
                end
            else
                distance = (position - playerHumanoidRootPart.Position).Magnitude
            end

            if nearestDistance and nearestDistance > distance then
                nearestCharacter = character
                nearestDistance = distance
            else
                nearestCharacter = character
                nearestDistance = distance
            end
        end
    end

    return nearestCharacter:WaitForChild("HumanoidRootPart")
end

It loops through the players from the players service and gets the character from the player variable. If you have alot of objects in the workspace it can take a really long time looping through every one of them so I recommend using the loop I used. The function also uses pathfinding if the path to the player is interrupted by an obstacle which makes the distance more accurate.

1 Like

its been 2 years… i dont think they even remember this post anymore

Wait what, I didn’t realise the post is 2 years old. I just clicked on a one that was similiar to what I was having trouble with.

can confirm i dont remember posting this
i know i solved the problem though i remember getting my ai to work

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.