local Players = game:GetService("Players") local NPC = script.Parent local HumanoidRootPart = NPC.HumanoidRootPart local Humanoid = NPC.Monster local function FindNearestCharacter(position: Vector3) local nearestCharacter = nil local nearestDistance = math.huge for i, player in Players:GetPlayers() do local character = player.Character local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart") local distance = humanoidRootPart and (humanoidRootPart.Position - position).Magnitude if distance and distance < nearestDistance then nearestCharacter = character nearestDistance = distance end end return nearestDistance, nearestCharacter end while task.wait() do local nearestDistance, nearestCharacter = FindNearestCharacter(HumanoidRootPart.Position) if nearestCharacter then local position = nearestCharacter.HumanoidRootPart.Position HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, Vector3.new(position.X, HumanoidRootPart.Position.Y, position.Z)) if nearestDistance < 3 then Humanoid:MoveTo(HumanoidRootPart.Position - HumanoidRootPart.CFrame.LookVector * 3) elseif nearestDistance > 3 then Humanoid:MoveTo(position - HumanoidRootPart.CFrame.LookVector * 4) end end end