Here is what I have so far (Server Script inside the rig's model:
local NPC = script.Parent
local Humanoid = script.Parent:FindFirstChild("Humanoid")
local HRP = script.Parent:FindFirstChild("HumanoidRootPart")
local function GetClosestPlayer(range)
local List = {}
local Target = nil
for i, v in pairs(game.Players:GetPlayers()) do
local dist = v:DistanceFromCharacter(HRP.position)
if dist <= range then
table.insert(List, {dist, v})
end
end
table.sort(List, function(A, B)
return A[1] < B[1]
end)
pcall(function()
Target = List[1][2]
end)
return Target
end
while true do
wait(0.3)
local Target = GetClosestPlayer(15)
if Target == nil then return end
local char = Target.Character
local hrp = char:WaitForChild("HumanoidRootPart")
Humanoid:MoveTo(hrp.Position)
Humanoid.MoveToFinished:Wait()
end
It errors:
Workspace.Dummy.Script:31: attempt to index nil with 'WaitForChild'

3 comments
Check whether Character is nil
You're attempting to index character with rbxscriptsignal. Make sure character is referenced.
It is.