Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PathfindingService = game:GetService("PathfindingService")
- local pirate = script.Parent
- local humanoid = pirate:WaitForChild("Humanoid")
- local rootPart = pirate:WaitForChild("HumanoidRootPart")
- local npcFolder = workspace:WaitForChild("NPC")
- local dataFolder = workspace:WaitForChild("Data")
- local walkSpeedValue = npcFolder:WaitForChild("WalkSpeed")
- local playerValue = dataFolder:WaitForChild("Player")
- local ATTACK_DISTANCE = 5
- local UPDATE_TIME = 0.4
- -- Для NPC лучше оставить управление физикой на сервере
- pcall(function()
- rootPart:SetNetworkOwner(nil)
- end)
- -- Создаём Animator, если его нет
- local animator = humanoid:FindFirstChildOfClass("Animator")
- if not animator then
- animator = Instance.new("Animator")
- animator.Parent = humanoid
- end
- --------------------------------------------------
- -- ПОИСК АНИМАЦИИ ХОДЬБЫ
- --------------------------------------------------
- local animateScript = pirate:FindFirstChild("Animate")
- local walkAnimation = nil
- if animateScript then
- -- Основной вариант:
- -- Animate > walk > WalkAnim
- local walkFolder = animateScript:FindFirstChild("walk")
- if walkFolder then
- walkAnimation = walkFolder:FindFirstChild("WalkAnim")
- end
- -- Если не нашли WalkAnim, ищем любую Animation
- -- внутри объекта walk
- if not walkAnimation and walkFolder then
- for _, object in ipairs(walkFolder:GetDescendants()) do
- if object:IsA("Animation") then
- walkAnimation = object
- break
- end
- end
- end
- -- Запасной вариант:
- -- Animate > run > RunAnim
- if not walkAnimation then
- local runFolder = animateScript:FindFirstChild("run")
- if runFolder then
- walkAnimation = runFolder:FindFirstChild("RunAnim")
- if not walkAnimation then
- for _, object in ipairs(runFolder:GetDescendants()) do
- if object:IsA("Animation") then
- walkAnimation = object
- break
- end
- end
- end
- end
- end
- end
- --------------------------------------------------
- -- ЗАГРУЖАЕМ АНИМАЦИЮ
- --------------------------------------------------
- local walkTrack = nil
- if walkAnimation and walkAnimation:IsA("Animation") then
- walkTrack = animator:LoadAnimation(walkAnimation)
- walkTrack.Looped = true
- walkTrack.Priority = Enum.AnimationPriority.Movement
- print("Анимация ходьбы найдена у", pirate.Name)
- print("AnimationId:", walkAnimation.AnimationId)
- else
- warn("У Pirate не найдена анимация WalkAnim!")
- end
- --------------------------------------------------
- -- ФУНКЦИИ ХОДЬБЫ
- --------------------------------------------------
- local function startWalking()
- if not walkTrack then
- return
- end
- if not walkTrack.IsPlaying then
- walkTrack:Play(0.15)
- end
- -- Стандартная скорость анимации рассчитана примерно на 16
- -- поэтому подстраиваем её под WalkSpeed.
- local animationSpeed = humanoid.WalkSpeed / 16
- walkTrack:AdjustSpeed(math.max(animationSpeed, 0.1))
- end
- local function stopWalking()
- if walkTrack and walkTrack.IsPlaying then
- walkTrack:Stop(0.15)
- end
- end
- --------------------------------------------------
- -- НАСТРОЙКА СКОРОСТИ
- --------------------------------------------------
- humanoid.WalkSpeed = walkSpeedValue.Value
- walkSpeedValue.Changed:Connect(function()
- if humanoid and humanoid.Parent then
- humanoid.WalkSpeed = walkSpeedValue.Value
- end
- end)
- --------------------------------------------------
- -- ПРЕСЛЕДОВАНИЕ
- --------------------------------------------------
- while pirate.Parent do
- if humanoid.Health <= 0 then
- stopWalking()
- break
- end
- -- Пока атакует, не двигаемся
- if pirate:GetAttribute("IsAttacking") then
- stopWalking()
- humanoid:MoveTo(rootPart.Position)
- task.wait(0.1)
- continue
- end
- local player = playerValue.Value
- if not player then
- stopWalking()
- task.wait(UPDATE_TIME)
- continue
- end
- local character = player.Character
- if not character then
- stopWalking()
- task.wait(UPDATE_TIME)
- continue
- end
- local targetHumanoid = character:FindFirstChildOfClass("Humanoid")
- local targetRoot = character:FindFirstChild("HumanoidRootPart")
- if not targetHumanoid or not targetRoot or targetHumanoid.Health <= 0 then
- stopWalking()
- task.wait(UPDATE_TIME)
- continue
- end
- local distance = (
- targetRoot.Position - rootPart.Position
- ).Magnitude
- --------------------------------------------------
- -- ДОШЛИ ДО ИГРОКА
- --------------------------------------------------
- if distance <= ATTACK_DISTANCE then
- stopWalking()
- humanoid:MoveTo(rootPart.Position)
- task.wait(0.1)
- continue
- end
- --------------------------------------------------
- -- ИДЁМ К ИГРОКУ
- --------------------------------------------------
- startWalking()
- local path = PathfindingService:CreatePath({
- AgentRadius = 2,
- AgentHeight = 5,
- AgentCanJump = true,
- WaypointSpacing = 3
- })
- local success = pcall(function()
- path:ComputeAsync(
- rootPart.Position,
- targetRoot.Position
- )
- end)
- if success and path.Status == Enum.PathStatus.Success then
- local waypoints = path:GetWaypoints()
- for i = 2, #waypoints do
- if pirate:GetAttribute("IsAttacking") then
- break
- end
- character = playerValue.Value.Character
- if not character then
- break
- end
- targetHumanoid = character:FindFirstChildOfClass("Humanoid")
- targetRoot = character:FindFirstChild("HumanoidRootPart")
- if not targetHumanoid or not targetRoot then
- break
- end
- if targetHumanoid.Health <= 0 then
- break
- end
- distance = (
- targetRoot.Position - rootPart.Position
- ).Magnitude
- -- Подошли достаточно близко
- if distance <= ATTACK_DISTANCE then
- break
- end
- local waypoint = waypoints[i]
- if waypoint.Action == Enum.PathWaypointAction.Jump then
- humanoid.Jump = true
- end
- startWalking()
- humanoid:MoveTo(waypoint.Position)
- local reached = humanoid.MoveToFinished:Wait()
- if not reached then
- break
- end
- end
- else
- -- Если Pathfinding не смог построить путь,
- -- идём прямо к игроку
- startWalking()
- humanoid:MoveTo(targetRoot.Position)
- end
- task.wait(UPDATE_TIME)
- end
Advertisement
Add Comment
Please, Sign In to add comment