local reaper = script.Parent
local humanoid = reaper.Humanoid
reaper.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = reaper.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - reaper.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, reaper)
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		else
			return false
		end
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (reaper.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")
	
	local pathParams = {
		["AgentHeight"] = 9.5,
		["AgentRadius"] = 4,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(reaper.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function attack(target)
	local distance = (reaper.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 3 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local attackAnim = humanoid:LoadAnimation(script.Attack)
		attackAnim:Play()
		target.Humanoid.Health = 0
	end
end

local function walkTo(destination)
	
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				attack(target)
				break
			else
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (reaper.HumanoidRootPart.CFrame.LookVector * 5))
	end
end

function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while true do
	wait(0.03)
	patrol()
end

I'm pretty sure this is a simple fix, but every time the AI and the player's humanoid root part gets 8 studs away, it stutters.