honestly Idk how many errors there are i got this online so please do a check for viruses as well thx! :)
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 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() local Target = getClosestPlayer(30) -- // change 30 to the max distance if Target == nil then return end Humanoid:MoveTo(Target.PrimaryPart) Humanoid.MoveToFinished:Wait() end
Ps: I dont have the errors I kinda left the game alone and never went back to it lol its somewhere XD