Making an NPC That Follows the Player

Learn how to create a simple NPC character that follows players around your game using Roblox's Humanoid and PathfindingService.

Ever wanted to create an NPC companion that follows your player around, or maybe an enemy that chases them? In this tutorial, we’ll build a simple following NPC from scratch. By the end, you’ll understand how to use Roblox’s built-in Humanoid and basic position tracking to create interactive characters!

What You’ll Need

Before we start scripting, let’s set up our NPC in Roblox Studio:

  • A basic NPC model with a Humanoid (you can insert a free rig from the Toolbox, or use the “Rig Builder” plugin)
  • A Script placed inside your NPC model (not a LocalScript – we want this to run on the server)

Make sure your NPC is positioned somewhere in your workspace where it can move around freely.

Understanding the Basics

Before we dive into code, let’s understand what we’re doing. Our NPC needs to:

  1. Find the nearest player
  2. Figure out where that player is
  3. Move toward the player’s position
  4. Keep doing this continuously

Roblox makes this easier than you might think! Every character model with a Humanoid has a built-in MoveTo() method that handles the walking animation and movement for us.

Writing the Script

Let’s start by getting references to the parts we need. Add this code to your Script inside the NPC:

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local rootPart = npc:WaitForChild("HumanoidRootPart")

Here’s what’s happening: We’re storing references to the NPC model, its Humanoid (which controls movement), and the HumanoidRootPart (the invisible part at the center of every character). We use WaitForChild() to make sure these parts have loaded before we try to use them.

Finding the Nearest Player

Now we need a function to find the closest player. Add this below your variables:

local function findNearestPlayer()
	local nearestPlayer = nil
	local shortestDistance = math.huge
	
	for _, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character
		if character and character:FindFirstChild("HumanoidRootPart") then
			local playerRoot = character.HumanoidRootPart
			local distance = (rootPart.Position - playerRoot.Position).Magnitude
			
			if distance < shortestDistance then
				shortestDistance = distance
				nearestPlayer = character
			end
		end
	end
	
	return nearestPlayer
end

This function loops through all players in the game and calculates the distance between the NPC and each player. The .Magnitude property gives us the distance between two positions. We keep track of which player is closest and return their character.

Making the NPC Follow

Now for the main loop that makes our NPC actually follow the player:

while true do
	local targetCharacter = findNearestPlayer()
	
	if targetCharacter then
		local targetRoot = targetCharacter:FindFirstChild("HumanoidRootPart")
		if targetRoot then
			humanoid:MoveTo(targetRoot.Position)
		end
	end
	
	wait(0.5)
end

This creates an infinite loop that continuously finds the nearest player and tells the NPC's Humanoid to move toward them. The wait(0.5) at the end means we update the target position every half second - this prevents the script from running too fast and causing performance issues.

The Complete Script

Here's everything together:

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local rootPart = npc:WaitForChild("HumanoidRootPart")

local function findNearestPlayer()
	local nearestPlayer = nil
	local shortestDistance = math.huge
	
	for _, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character
		if character and character:FindFirstChild("HumanoidRootPart") then
			local playerRoot = character.HumanoidRootPart
			local distance = (rootPart.Position - playerRoot.Position).Magnitude
			
			if distance < shortestDistance then
				shortestDistance = distance
				nearestPlayer = character
			end
		end
	end
	
	return nearestPlayer
end

while true do
	local targetCharacter = findNearestPlayer()
	
	if targetCharacter then
		local targetRoot = targetCharacter:FindFirstChild("HumanoidRootPart")
		if targetRoot then
			humanoid:MoveTo(targetRoot.Position)
		end
	end
	
	wait(0.5)
end

Testing Your NPC

Press Play and watch your NPC come to life! It should immediately start walking toward your character. Try running away - the NPC will follow you. If you have multiple players in your game, it will always follow whoever is closest.

Customizing Your NPC

Want to make your NPC faster or slower? Change the Humanoid's WalkSpeed property by adding this line after you define the humanoid variable:

humanoid.WalkSpeed = 20 -- Default is 16

You can also adjust how often the NPC updates its target by changing the number in wait(0.5). Smaller numbers make it more responsive but use more resources.

Recap and Next Steps

Congratulations! You've created your first following NPC. You learned how to find the nearest player using distance calculations, use the Humanoid's MoveTo method, and create a continuous update loop.

Ready to take this further? Try learning about PathfindingService next - it allows NPCs to navigate around obstacles intelligently instead of walking straight through walls. You could also explore how to make the NPC stop following when it gets close enough, or create different behaviors based on distance!

Author

Leave a Reply

Your email address will not be published. Required fields are marked *