How do I make an npc follow the closest player?

So here, what I am trying to achieve, is to have a cutscene (which i can do myself) and have an npc come out of a place and start to attack the closest player, and it dies after it is killed it has an animation. But I don’t have any clue on what to do to get it to attack the closest player. I can’t come up with any code to do this. I would appreciate any help here, doesn’t have to be code, but it can be another resource too.

6 Likes
3 Likes

Easy! Get the position of all the players on the server, and get the current position of the NPC. Iterate through the list of players and find their magnitudes. Then, get the player with the least magnitude and use the pathfinding API to move the humanoid based NPC there.

Edit: I wrote the AI for you because I was bored :confused:

local PathfindingService = game:GetService("PathfindingService")

local npc = game.Workspace.NPC
local humanoid = npc.Humanoid

function goToNearsetPlayer ()
	local currentPos = humanoid.RootPart.Position
	local players = game.Players:GetChildren()
	local playerPoss = {}
	local playerMags = {}
	local minMag
	
	-- get their positions
	for index, value in pairs(players) do
		playerPoss[#playerPoss + 1] = value.Character.Humanoid.RootPart.Position
	end
	
	-- find the magnitudes
	for index, value in pairs(playerPoss) do
		playerMags[#playerMags + 1] = (currentPos - value).Magnitude
	end
	
	-- find the minimum
	for index, value in pairs(playerMags) do
		minMag = math.min(minMag, value)
	end
	
	-- go now
	for index, value in pairs(playerMags) do
		if minMag == value then
			local goToPlayer = players[index]
			local path = PathfindingService:CreatePath(putYourParamsHere)
			
			path:ComputeAsync(currentPos, goToPlayer.Character.Humanoid.RootPart.Position)
			local waypoints = path:GetWaypoints()
			
			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
				
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
			
			-- end the loop
			break
		end
	end
end
10 Likes

Where would I add this script?

Idk, it’s your call. I would put it inside the NPC model instance.

Edit: Remember, this code will not work! You will have to change the local values and stuff to actually utilize it. This is just a demo!

2 Likes

Thanks for helping! Whenever I am able to I will test it.

Seems like you’re new. If you get a satisfactory answer from someone, click the Solution button so that people can find the answer easily and do not have to read the whole thread. The button looks like this:

1 Like

the script not working for me!!!
!

1 Like

theres a problem, at the math.min part minMag isnt a number and it hasnt been assigned yet

Then that means there are no NPCs

that doesnt make sense, it says minMag isnt a number because it isnt assigned in the first place

minMag will be assigned upon iteration over the first NPC