NPC follow script not working

I am making a NPC follow script and I have no idea why this is not working. I see no errors in the output.

function followPlayers()
	local function findClosestPlayer()
		local closestCharacter = nil
		for i,v in pairs(workspace:GetChildren()) do
			if v:FindFirstChild("Humanoid") and v ~= npc and game.Players:GetPlayerFromCharacter(v) then
				local character = v
				local player = game.Players:GetPlayerFromCharacter(character)
				if character.HumanoidRootPart then
					if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude < 100 then
						closestCharacter = character
					end
				end
				while true do
					if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude > 10 then
						findClosestPlayer()
						npc.Humanoid:MoveTo(closestCharacter.HumanoidRootPart.Position, closestCharacter)
					end
					wait(0.2)
				end
			end
		end
	end
end
1 Like

Have you tried print debugging first?

1 Like

I am new. What’s a print debugging?
Edit: okay nevermind ill try

Did you call the function at all? From this script you just have a written function without calling it.
Is the script you provided the entire script or just part of it?

1 Like

Might be a good idea to call the function, otherwise it’s never going to be run!

2 Likes

Just a part, I used spawn(followPlayers) at the end.
Here’s the whole script (it looks different now since I am trying to make it work):

local RunService = game:GetService("RunService")

local npc = script.Parent

function followPlayers()
	while wait(0.2) do
		local function findClosestPlayer()
		local closestCharacter = nil
		for i,v in pairs(workspace:GetChildren()) do
				if v:FindFirstChild("Humanoid") and v ~= npc and game.Players:GetPlayerFromCharacter(v) then
					local character = v
					local player = game.Players:GetPlayerFromCharacter(character)
					if character.HumanoidRootPart then
						if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude < 100 then
							closestCharacter = character
						end
					end
					if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude > 10 then
						findClosestPlayer()
						npc.Humanoid:MoveTo(closestCharacter.HumanoidRootPart.Position, closestCharacter)
					end
				end
			end
		end
	end
end

spawn(followPlayers)

Okay, if that doesn’t work maybe try this:

local npc = script.Parent
local ClosestPlayer

function findPlayers()
   for i,v in pairs(workspace:GetChildren()) do
     if v:FindFirstChild("Humanoid") and v ~= npc and game.Players:GetPlayerFromCharacter(v) then
        if (npc.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude > 10 then
            ClosestPlayer = v
	    end
     end
   end
end

function followPlayers()
   npc.Humanoid:MoveTo(ClosestPlayer.HumanoidRootPart.Position)
end

while wait() do
   findPlayers()
   followPlayers()
end

Try that and let me know if you get any errors.

1 Like

I’ll try this after i do one thing, I just realized I didn’t spawn(findClosestPlayer). Idiot me.

I will use your script as plan B. Sorry for that but I want to script something by my own.
So… I forgot to add spawn(findClosestPlayer), so I added it. Now while I run the game, studio launcher is lagging extremely and sometimes I get this error:


The script looks like this now:

function followPlayers()
	while wait(0.2) do
		local function findClosestPlayer()
			local closestCharacter = nil
			for i,v in pairs(workspace:GetChildren()) do
				if v:FindFirstChild("Humanoid") and v ~= npc and game.Players:GetPlayerFromCharacter(v) then
					local character = v
					local player = game.Players:GetPlayerFromCharacter(character)
					if character.HumanoidRootPart then
						if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude < 100 then
							closestCharacter = character
						end
					end
					if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude > 10 then
						findClosestPlayer()
						npc.Humanoid:MoveTo(closestCharacter.HumanoidRootPart.Position, closestCharacter)
					end
				end
			end
		end
		spawn(findClosestPlayer)
	end
end

spawn(lookingPlayers)
spawn(followPlayers)

Any idea how to fix it?

Okay it works! Thank you all very much.

Script if anyone needs it in the future:

function followPlayers()
	while wait(0.2) do
		local function findClosestPlayer()
			local closestCharacter = nil
			for i,v in pairs(workspace:GetChildren()) do
				if v:FindFirstChild("Humanoid") and v ~= npc and game.Players:GetPlayerFromCharacter(v) then
					local character = v
					local player = game.Players:GetPlayerFromCharacter(character)
					if character.HumanoidRootPart then
						if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude < 100 then
							closestCharacter = character
						end
					end
					if (npc.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude > 10 then
						npc.Humanoid:MoveTo(closestCharacter.HumanoidRootPart.Position, closestCharacter)
					end
				end
			end
		end
		spawn(findClosestPlayer)
	end
end

spawn(lookingPlayers)
spawn(followPlayers)