local npc = script.Parent
local humanoid = npc:FindFirstChildOfClass("Humanoid")
local hrp = npc:FindFirstChild("HumanoidRootPart")
local tool = npc:FindFirstChildOfClass("Tool") or npc:FindFirstChildOfClass("AK-74")
if not humanoid or not hrp or not tool then
warn("NPC setup error: missing humanoid, HRP or tool.")
return
end
-- Безопасность
hrp.Anchored = false
humanoid.PlatformStand = false
humanoid.WalkSpeed = 16
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
-- Настройки
local SHOOT_RANGE = 40
local FOLLOW_RANGE = 80
local REPATH_INTERVAL = 1.5
local PATROL_RADIUS = 50
local PATROL_WAIT = 2
local lastPathTime = 0
local currentTarget = nil
local startPos = hrp.Position
-- Найти ближайшего игрока
local function getNearestPlayer()
local closest, dist = nil, FOLLOW_RANGE
for _, plr in ipairs(Players:GetPlayers()) do
local char = plr.Character
if char and char:FindFirstChild("HumanoidRootPart") and char:FindFirstChild("Humanoid") then
if char.Humanoid.Health > 0 then
local d = (char.HumanoidRootPart.Position - hrp.Position).Magnitude
if d < dist then
dist = d
closest = char
end
end
end
end
return closest
end
-- Создать временную цель в мире
local function createWaypoint()
local offset = Vector3.new(
math.random(-PATROL_RADIUS, PATROL_RADIUS),
0,
math.random(-PATROL_RADIUS, PATROL_RADIUS)
)
local position = startPos + offset
local waypoint = Instance.new("Part")
waypoint.Anchored = true
waypoint.CanCollide = false
waypoint.Transparency = 1
waypoint.Size = Vector3.new(1, 1, 1)
waypoint.Position = position + Vector3.new(0, 2, 0) -- Чуть выше земли
waypoint.Name = "PatrolPoint"
waypoint.Parent = Workspace
game:GetService("Debris"):AddItem(waypoint, 10) -- удалится через 10 сек
return waypoint
end
-- Патруль
local function patrol()
print("[NPC] Patrolling...")
local wp = createWaypoint()
humanoid:MoveTo(wp.Position)
local reached = humanoid.MoveToFinished:Wait(5)
if reached then
print("[NPC] Reached patrol point.")
else
print("[NPC] Didn't reach point.")
end
task.wait(PATROL_WAIT)
end
-- Главный цикл
RunService.Heartbeat:Connect(function()
if humanoid.Health <= 0 then return end
local target = getNearestPlayer()
if target then
local tHRP = target:FindFirstChild("HumanoidRootPart")
if not tHRP then return end
local distance = (tHRP.Position - hrp.Position).Magnitude
print("[NPC] Target found, distance:", distance)
tool:SetAttribute("Target", tHRP)
if distance > SHOOT_RANGE then
if tick() - lastPathTime >= REPATH_INTERVAL then
lastPathTime = tick()
humanoid:MoveTo(tHRP.Position)
end
else
humanoid:MoveTo(hrp.Position)
end
else
if tick() - lastPathTime >= REPATH_INTERVAL then
lastPathTime = tick()
patrol()
end
end
end)
У меня есть код на нпс с патрулем. В общем, надо, что бы он был более живим, что я имею ввиду: Каждый раз были не такие резки повороты, всегда по разному. Поле пройденого расстояния, перед тем как поменять направление, он останавливался, и, "осматривался" смотря то в 1 то в другую сторону (минимальная разница осмотра 30 градусов). Также можешь чего то от себя дать