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
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?
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)
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
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:
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)
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)