local npc = script.Parent local humanoid = npc:FindFirstChildOfClass("Humanoid") local rootPart = npc:FindFirstChild("HumanoidRootPart") local DAMAGE = 10 local ATTACK_RANGE = 5 local FOLLOW_DISTANCE = 20 local HEALTH = 100 local JUMP_COOLDOWN = 2 local RESPAWN_DELAY = 5 local RESPAWN_RADIUS = 10 -- радиус, в котором NPC появится заново local Players = game:GetService("Players") local Workspace = game:GetService("Workspace") -- Сохраняем исходную позицию NPC local spawnPosition = rootPart.Position local lastJumpTime = 0 local alive = true local function getNearestPlayer() local nearestPlayer = nil local nearestDistance = math.huge for _, player in pairs(Players:GetPlayers()) do local character = player.Character if character and character:FindFirstChild("HumanoidRootPart") and character:FindFirstChildOfClass("Humanoid") then local distance = (character.HumanoidRootPart.Position - rootPart.Position).magnitude if distance < nearestDistance then nearestDistance = distance nearestPlayer = character end end end return nearestPlayer, nearestDistance end local function attack(targetHumanoid) if targetHumanoid and targetHumanoid.Health > 0 then targetHumanoid:TakeDamage(DAMAGE) end end local function isObstacleAhead() local rayOrigin = rootPart.Position local rayDirection = rootPart.CFrame.LookVector * 4 local raycastParams = RaycastParams.new() raycastParams.FilterDescendantsInstances = {npc} raycastParams.FilterType = Enum.RaycastFilterType.Blacklist local result = Workspace:Raycast(rayOrigin, rayDirection, raycastParams) if result then local hitPos = result.Position local heightDifference = hitPos.Y - rootPart.Position.Y if heightDifference > 0.5 then return true end end return false end -- Функция для респауна NPC local function respawn() alive = false humanoid.WalkSpeed = 0 humanoid:MoveTo(rootPart.Position) -- остановить движение wait(RESPAWN_DELAY) -- Случайное смещение в пределах RESPAWN_RADIUS по горизонтали local offsetX = (math.random() - 0.5) * 2 * RESPAWN_RADIUS local offsetZ = (math.random() - 0.5) * 2 * RESPAWN_RADIUS local newPos = spawnPosition + Vector3.new(offsetX, 0, offsetZ) -- Перемещаем NPC и восстанавливаем здоровье npc:SetPrimaryPartCFrame(CFrame.new(newPos)) humanoid.Health = humanoid.MaxHealth humanoid.WalkSpeed = 16 -- стандартная скорость движения (можно настроить) alive = true end -- Обработчик смерти humanoid.Died:Connect(function() respawn() end) -- Инициализация if humanoid then humanoid.MaxHealth = HEALTH humanoid.Health = HEALTH humanoid.WalkSpeed = 16 else humanoid = Instance.new("Humanoid") humanoid.MaxHealth = HEALTH humanoid.Health = HEALTH humanoid.WalkSpeed = 16 humanoid.Parent = npc end while true do if alive and humanoid.Health > 0 then local target, distance = getNearestPlayer() if target and distance <= FOLLOW_DISTANCE then humanoid:MoveTo(target.HumanoidRootPart.Position) if tick() - lastJumpTime >= JUMP_COOLDOWN and isObstacleAhead() then humanoid.Jump = true lastJumpTime = tick() end if distance <= ATTACK_RANGE then attack(target:FindFirstChildOfClass("Humanoid")) end else humanoid:MoveTo(rootPart.Position) end end wait(0.3) end