Pathfinding NPC following the player but on their head?

You can write your topic however you want, but you need to answer these questions:

  1. Goal
    I want to make a smooth pathfinding script that follows the player instead of stays on the players head.
  2. What is the issue?
    https://gyazo.com/b9c348f043b1b6ee5f60e5fbefffe184 – As you can see the NPC is on top of the players head when following the player.
  3. What solutions have you tried so far?
    Changing parts in the script to see if it changed anything but it did not. Not sure what the problem is.
local leftarm = script.Parent:FindFirstChild("UpperTorso")
local rightarm = script.Parent:FindFirstChild("RootPart")

function findNearestTorso(pos)
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 60
	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)
	end

end

If you could help that would be great!

From what i see, the NPC is TELEPORTING to the player. And since the NPC doesn’t has the Anchored property set to true, it will teleport above the player, since it won’t be stuck inside of its character ;/ .

Also, i didn’t even see you using game:GetService("PathfindingService") in the code.

You could maybe add a Vector3 like this might have to adjust it

local leftarm = script.Parent:FindFirstChild("UpperTorso")
local rightarm = script.Parent:FindFirstChild("RootPart")

function findNearestTorso(pos)
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 60
	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 - Vector3.new(5 ,5, 0), target)
	end
end

Alright. I will try that solution.

Thanks for all of the help. It did work to keep the NPC on the ground. Thanks.