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

4 comments
"just fix them" isn't the best way to learn or get help.
like Exanut said, don't just come here asking for people to fix your script. Also, if you even want any help on how to fix it atleast provide a picture, or maybe just type here what the errors are.
sorry i know that was a bad idea
it would be better to ask for help by showing the code then the errors that your getting in output