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

0

"just fix them" isn't the best way to learn or get help.

0

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.

0

sorry i know that was a bad idea

0

it would be better to ask for help by showing the code then the errors that your getting in output