How to have NPC's Look at you and Follow you until a certain distance

Hello there, I’m somewhat new still to scripting on Roblox and creating stuff but currently there’s been a problem that I’ve been trying to figure out for about 3-4 hours now.
My idea for my game currently is to have some party members and such join you on your adventure and essentially follow you. My idea is that the party members follow you around in sort of a conga line sort of deal except like 5-10 studs behind one another with there being multiple members obviously.
Some of my first ideas on trying to figure this entire thing out was to originally try to get the player position on a server-side script but for the life of me I can’t figure out how and then also try to figure out how to have the NPC’s turn towards the player and walk towards the player and then not moving forwards anymore until the player gets farther away.

Any help with this would be very appreciated as I can’t seem to figure this issue out that easily.

Utilise this: How to create a ball that tracks the nearest player and follows them around - #2 by WolfieCodesMent

You need some variables:

local MinDistance = YourDistance
local Player = AnyWayYouGetThePlayer
local Distance = (NPC.Position - Player.HumanoidRootPart.Position) * Magnitude

I don’t want to write more down because I’m tired and my brain doesn’t want to work

Thank you very much for this, I’ll try and out and let you know if there are any issues or questions.

With mine, you can download the .rbxm file and then implement it into Roblox Studio.
And from there, you can then import a new character rig and put the script from my model into the new character rigs humanoid.

Script can be found inside of the old model:
image

And no problem, you’re welcome.

Alright well I tried out just getting the code and giving it a look over and such and nothing seems to be out of the ordinary except for when I’m actually testing out the code within the game. So whenever the npc now moves there aren’t any animations currently and they’re just sort of hovering towards the player. Along with this they seem to move oddly at first but after some research I think this may have been due to an error on my end with tweening the humanoid root part position without using CFrames.
With all that said everything else seems to be in good shape and all I have to figure out now is how to be able to toggle when the NPC’s follow the player.

What I would do is create an attribute that gets updated whenever sowntign you want happens, and if true make them follow or else not

I see, that seems pretty simple to add too so not an issue.
Now what exactly would I need to do to make the NPC also have a specific walk animation while they’re on the move?

You have to create one and make it play when they walk, teres different ways of doing it, just give a look on the Internet

Alright then, thank you for the help.

“there aren’t any animations currently”
Well that’s expected because it isn’t playing any animations when moving…

Use this article for pathfinding:
Character Pathfinding | Roblox Creator Documentation

Also, here is an example script that I use in my game for spiders. This does have a few issues but it should help you get started.

-- Services
local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")

-- Parts associated with the AI character
local rig = script.Parent
local humanoid = script.Parent:WaitForChild("Humanoid")
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local torso = script.Parent:WaitForChild("Torso")


-- Config for character pathfinding
local pathArgs = {
	["AgentRadius"] = 2,
	["AgentHeight"] = 1
}

-- View distance
-- The AI can see things that are this number of studs away
local max_range = 20

-- Sounds
local crawl_sound = torso:WaitForChild("crawl_noise")

-- Animations
local animator = humanoid:WaitForChild("Animator")
local is_walking = false
local walkAnimation = Instance.new("Animation")
walkAnimation.AnimationId = "rbxassetid://11489229503"
local walkAnimationTrack = animator:LoadAnimation(walkAnimation)

-- Check the distance from a Part (i.e. a player's torso) to the HumanoidRootPart of the npc
function getDistance(part)
	return (humanoidRootPart.Position - part.Position).Magnitude
end

-- The AI will chase one of the players if the player is within range
function findTarget()
	local dist = max_range
	local target = nil
	
    -- Find if any of the people in the game are close enough to the ai
	for i, v in ipairs(Players:GetPlayers()) do
		local character = v.Character or v.CharacterAdded:Wait()
		local human = character:WaitForChild("Humanoid")
		local torso = character:WaitForChild("HumanoidRootPart")
		
        -- if the person is within range and not dead, they are the new target
		if human and torso and v.Name ~= script.Parent.Name then
			if getDistance(torso) < 5 then
				humanoid.Jump = true
			end
			if getDistance(torso) < dist and human.Health > 0 then
				target = torso
				dist = getDistance(torso)
			end
		end
	
	end

	return target
end


function goToTarget(target)
	if target then

		local path = (PathfindingService:CreatePath(pathArgs))
		path:ComputeAsync(humanoidRootPart.Position, target.Position)
		if path.Status == Enum.PathStatus.Success then
			local waypoints = path:GetWaypoints()
			for i, v in ipairs(waypoints) do
				-- Jump if needed
				if v.Action == Enum.PathWaypointAction.Jump then
					humanoid.Jump = true
				end
				
				-- Move to the waypoint
				humanoid:MoveTo(v.Position)
			end
                        walkAnimationTrack:Play()
		end

	end
end

-- Find a target for the ai character to chase after
-- Make the ai chase the found target
-- Do this 10 times every second
while true do
	local target = findTarget()
	goToTarget(target)
	wait(0.1)
end