How would I make a Npc follow the Closet Player?

So I wrote this code to follow the closet Player but… It doesn’t work and I’m not sure how to fix it Here is the code.

	local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
	local PathfindingService = game:GetService("PathfindingService")

	function findNearestTorso(pos)
		local list = game.Workspace:children()
		local torso = nil
		local dist = 100000000
		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(math.random(1,1))
		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
		if target ~= nil then

			local path = PathfindingService:CreatePath()


			-- Get the path waypoints
			path:ComputeAsync(target.Position,Temp.HumanoidRootPart)
			local waypoints = path:GetWaypoints()

			-- Loop through waypoints
			for _, waypoint in pairs(waypoints) do
				local part = Instance.new("Part")
				part.Shape = "Ball"
				part.Material = "Neon"
				part.Size = Vector3.new(0.6, 0.6, 0.6)
				part.Position = waypoint.Position
				part.Anchored = true
				part.CanCollide = false
				part.Parent = game.Workspace

				script.Parent.Humanoid:MoveTo(waypoints.Position)
				script.Parent.Humanoid.MoveToFinished:Wait()
			end

		end

	end

23:00:15.248 Workspace.R15.Script:40: attempt to index nil with ‘HumanoidRootPart’ - Server - Script:40

path:ComputeAsync(target.Position,Temp.HumanoidRootPart)

You didn’t define Temp Anywhere, that’s what I could see being the issue, did you forget a step for this?

So i shouldnt use temp? I should use temp2

Temp and Temp2 are local to the function, you have to use something else

what can i use can you give a example LOL

Use this maybe?

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

So it starts from the npc and then ends at the target

well now its like theres no pathfinding

Maybe trying switching them around?

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

also one of the lines i replaced with this

script.Parent.Humanoid:MoveTo(target.Position)

Wait what’s your code right now?

local PathfindingService = game:GetService("PathfindingService")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100000000
	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(math.random(1,1))
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then

		local path = PathfindingService:CreatePath()


		-- Get the path waypoints
		path:ComputeAsync(script.Parent.HumanoidRootPart.Position,target.Position)
		local waypoints = path:GetWaypoints()

		-- Loop through waypoints
		for _, waypoint in pairs(waypoints) do
			local part = Instance.new("Part")
			part.Shape = "Ball"
			part.Material = "Neon"
			part.Size = Vector3.new(0.6, 0.6, 0.6)
			part.Position = waypoint.Position
			part.Anchored = true
			part.CanCollide = false
			part.Parent = game.Workspace

			script.Parent.Humanoid:MoveTo(target.Position)
			
			
		end

	end

end

Try out the change I mentioned by swapping the 2 around?

Well now his just standing there menacingly

image

1 Like

Okay wait I realised what’s wrong. Return it back to how it was before

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

And change this

script.Parent.Humanoid:MoveTo(target.Position)

To this

script.Parent.Humanoid:MoveTo(waypoint.Position)

It wasn’t working cause it was trying to directly go to you rather than go from the waypoints

well i also had to add this script.Parent.Humanoid.MoveToFinished:Wait() but the Ai is really buggy are you able to join game and I show?

I could do that, link me it so I can see what’s happening

Ai for a game - Roblox Ok join me

You joining the game? :sweat_smile:

1 Like

Sorry! I was doing something! I’m getting on now!