NPC following player

Heyo, I was thinking about making a game that involves a mechanic with several NPCs following the player, somewhat like the famous game Earthbound/Mother 2’s party member system. It’s not about the health/GUIs that revolve around it, it’s the NPCs following the player. Below would be a GIF of how it would work. I thought of using PathfindingService but I wanted to know any other methods for this mechanic.

gyazo link: https://gyazo.com/904e6568e83cd70e7e7502fa03dd806c

1 Like

well I have some sort of a way that makes an NPC follow a player. something like this should work:

script.Parent.Humanoid.Died:Connect(function()
	wait(game.Players.RespawnTime)
	local newclone = game.ServerStorage["Test NPC"]:Clone()
	newclone.Movement.Disabled = false
	newclone.Parent = workspace
	script.Parent:Destroy()
end)

local playerfound = false
local player = nil
local normalmove = true

function playerfind()
	player = nil
	playerfound = false
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character then
			local plrpos = v.Character.HumanoidRootPart.CFrame.Position
			local npcpos = script.Parent.Head.CFrame.Position
			if (npcpos - plrpos).Magnitude <= 40 then
				playerfound = true
				player = v.Character
			end
		end
	end
end



while wait(0.25) do
	playerfind()
	if playerfound and player then
		script.Parent.Humanoid.WalkToPoint = player.HumanoidRootPart.Position
	else
		if normalmove then
			local x = math.random(5, 10)
			local y = script.Parent.Head.Position.Y
			local z = math.random(5, 10)
			script.Parent.Humanoid.WalkToPoint = script.Parent.Head.Position + Vector3.new(x, y, z)
			normalmove = false
			spawn(function()
				wait(5)
				normalmove = true
			end)
		end
	end
end

As I said, it should be something likes this. I gave you a script I had made for an NPC

1 Like

Thank you for the script, but I meant by permanently following the player, but just behind the player by a few studs. Should I just make blocks and then use PathfindingService to make the NPCs follow the blocks, making it look like they are following the player?

well I was thinking you would do a magnitude check of if the magnitude isn’t greater than about 3 or 4 from the player. Instead of checking if it’s not greater than something.

I don’t understand, are we on the same topic? I meant as in the gyazo link, they’re always behind the player by a few studs.

yes I am on the same topic as you. I will show what I mean by updating the script I had previously provided:

script.Parent.Humanoid.Died:Connect(function()
	wait(game.Players.RespawnTime)
	local newclone = game.ServerStorage["Test NPC"]:Clone()
	newclone.Movement.Disabled = false
	newclone.Parent = workspace
	script.Parent:Destroy()
end)

local playerfound = false
local player = nil
local normalmove = true

function playerfind()
	player = nil
	playerfound = false
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character then
			local plrpos = v.Character.HumanoidRootPart.CFrame.Position
			local npcpos = script.Parent.Head.CFrame.Position
			if (npcpos - plrpos).Magnitude >= 4 then --I changed this to check if it is right behind/next to the player.
				playerfound = true
				player = v.Character
			end
		end
	end
end



while wait(0.25) do
	playerfind()
	if playerfound and player then
		script.Parent.Humanoid.WalkToPoint = player.HumanoidRootPart.Position
	else
		if normalmove then
			local x = math.random(5, 10)
			local y = script.Parent.Head.Position.Y
			local z = math.random(5, 10)
			script.Parent.Humanoid.WalkToPoint = script.Parent.Head.Position + Vector3.new(x, y, z)
			normalmove = false
			spawn(function()
				wait(5)
				normalmove = true
			end)
		end
	end
end

I’m not talking about if the NPC is close to the player and it moves to them when it is, I meant as in the people are in a line and as the main leader walks, the followers follow them. Try searching up “Earthbound” or “Mother 3” if you might not get what I’m saying.

Yes, I get what you are saying I am just showing you a way to move the NPC #help-and-feedback:scripting-support isn’t for full scripts and (not trying to be lazy) I don’t want to make a whole new script when its like 1:00 AM my time.

Oh! I’m not in the right topic as you are in, you meant as in a reference to your own script. Sorry if I looked like I was wanting a whole script, I was wondering about the mechanic.

You could try recording the position of the player every unit of time, and then tell the NPC to move to that position. Also, to prevent the npc from trying to push the player around while the player is standing still, I also suggest implementing a magnitude check to see whether the NPC is far enough from the player before it starts moving.

Something like this:

local playerPos = torsoPart.Position
while true do
    wait(timeUnit)
    if (npc.Torso.Position - playerPos.Position).magnitude > distance then
        npc.Humanoid:MoveTo(playerPos)
        playerPos = torsoPart.Position
    end
end

well, the mechanic I used was to just set the Humanoid’s WalkToPoint property to the position of the player’s character and make sure of the distance before moving it more.