Hasli4

Untitled

Oct 4th, 2026
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. local pirate = script.Parent
  2. local humanoid = pirate:WaitForChild("Humanoid")
  3. local rootPart = pirate:WaitForChild("HumanoidRootPart")
  4.  
  5. local npcFolder = workspace:WaitForChild("NPC")
  6. local dataFolder = workspace:WaitForChild("Data")
  7.  
  8. local attackSpeedValue = npcFolder:WaitForChild("Attackspeed")
  9. local playerValue = dataFolder:WaitForChild("Player")
  10.  
  11. local ATTACK_DISTANCE = 5
  12. local DAMAGE = 10
  13. local HIT_DELAY = 0.25
  14.  
  15. local animator = humanoid:FindFirstChildOfClass("Animator")
  16.  
  17. if not animator then
  18. animator = Instance.new("Animator")
  19. animator.Parent = humanoid
  20. end
  21.  
  22. -- Ищем анимацию атаки
  23. local attackAnimation = nil
  24.  
  25. -- Сначала ищем Animation с названием Attack
  26. for _, object in ipairs(pirate:GetDescendants()) do
  27. if object:IsA("Animation") and object.Name == "Attack" then
  28. attackAnimation = object
  29. break
  30. end
  31. end
  32.  
  33. -- Если Attack нет, ищем Slash.
  34. -- У ClassicSword обычно именно такая анимация.
  35. if not attackAnimation then
  36. local sword = pirate:FindFirstChild("ClassicSword")
  37.  
  38. if sword then
  39. attackAnimation = sword:FindFirstChild("Slash")
  40.  
  41. if not attackAnimation then
  42. for _, object in ipairs(sword:GetDescendants()) do
  43. if object:IsA("Animation") and object.Name == "Slash" then
  44. attackAnimation = object
  45. break
  46. end
  47. end
  48. end
  49. end
  50. end
  51.  
  52. -- Последняя попытка — найти любую Animation внутри Pirate
  53. if not attackAnimation then
  54. for _, object in ipairs(pirate:GetDescendants()) do
  55. if object:IsA("Animation") then
  56. attackAnimation = object
  57. break
  58. end
  59. end
  60. end
  61.  
  62. local attackTrack = nil
  63.  
  64. if attackAnimation then
  65. attackTrack = animator:LoadAnimation(attackAnimation)
  66. attackTrack.Priority = Enum.AnimationPriority.Action
  67. else
  68. warn("У Pirate не найдена анимация атаки!")
  69. end
  70.  
  71. while pirate.Parent do
  72.  
  73. if humanoid.Health <= 0 then
  74. break
  75. end
  76.  
  77. local player = playerValue.Value
  78.  
  79. if not player then
  80. task.wait(0.2)
  81. continue
  82. end
  83.  
  84. local character = player.Character
  85.  
  86. if not character then
  87. task.wait(0.2)
  88. continue
  89. end
  90.  
  91. local targetHumanoid = character:FindFirstChildOfClass("Humanoid")
  92. local targetRoot = character:FindFirstChild("HumanoidRootPart")
  93.  
  94. if not targetHumanoid or not targetRoot or targetHumanoid.Health <= 0 then
  95. task.wait(0.2)
  96. continue
  97. end
  98.  
  99. local distance = (targetRoot.Position - rootPart.Position).Magnitude
  100.  
  101. if distance <= ATTACK_DISTANCE then
  102.  
  103. -- Сообщаем SeekEnemy, что сейчас атакуем
  104. pirate:SetAttribute("IsAttacking", true)
  105.  
  106. -- Поворачиваем пирата лицом к игроку
  107. local lookPosition = Vector3.new(
  108. targetRoot.Position.X,
  109. rootPart.Position.Y,
  110. targetRoot.Position.Z
  111. )
  112.  
  113. rootPart.CFrame = CFrame.lookAt(
  114. rootPart.Position,
  115. lookPosition
  116. )
  117.  
  118. -- Запускаем анимацию
  119. if attackTrack then
  120. attackTrack:Play()
  121. end
  122.  
  123. -- Небольшая задержка перед попаданием
  124. task.wait(HIT_DELAY)
  125.  
  126. -- Проверяем расстояние еще раз,
  127. -- потому что игрок мог отойти во время замаха
  128. if targetHumanoid.Health > 0 and targetRoot.Parent then
  129.  
  130. local newDistance = (
  131. targetRoot.Position - rootPart.Position
  132. ).Magnitude
  133.  
  134. if newDistance <= ATTACK_DISTANCE then
  135. targetHumanoid:TakeDamage(DAMAGE)
  136. end
  137. end
  138.  
  139. -- Ждем окончания анимации
  140. if attackTrack then
  141. local animationLength = attackTrack.Length
  142.  
  143. if animationLength > 0 then
  144. task.wait(math.max(animationLength - HIT_DELAY, 0))
  145. else
  146. task.wait(0.2)
  147. end
  148. else
  149. task.wait(0.2)
  150. end
  151.  
  152. pirate:SetAttribute("IsAttacking", false)
  153.  
  154. -- Скорость атаки.
  155. -- Например:
  156. -- 1 = атака раз в секунду
  157. -- 2 = две атаки в секунду
  158. -- 4 = четыре атаки в секунду
  159.  
  160. local attackSpeed = math.max(attackSpeedValue.Value, 1)
  161. local cooldown = 1 / attackSpeed
  162.  
  163. task.wait(cooldown)
  164.  
  165. else
  166. task.wait(0.1)
  167. end
  168. end
  169.  
Advertisement
Add Comment
Please, Sign In to add comment