Hasli4

Untitled

Oct 4th, 2026
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. local PathfindingService = game:GetService("PathfindingService")
  2.  
  3. local pirate = script.Parent
  4. local humanoid = pirate:WaitForChild("Humanoid")
  5. local rootPart = pirate:WaitForChild("HumanoidRootPart")
  6.  
  7. local npcFolder = workspace:WaitForChild("NPC")
  8. local dataFolder = workspace:WaitForChild("Data")
  9.  
  10. local walkSpeedValue = npcFolder:WaitForChild("WalkSpeed")
  11. local playerValue = dataFolder:WaitForChild("Player")
  12.  
  13. local ATTACK_DISTANCE = 5
  14. local UPDATE_TIME = 0.4
  15.  
  16. -- Для NPC лучше оставить управление физикой на сервере
  17. pcall(function()
  18. rootPart:SetNetworkOwner(nil)
  19. end)
  20.  
  21. -- Создаём Animator, если его нет
  22. local animator = humanoid:FindFirstChildOfClass("Animator")
  23.  
  24. if not animator then
  25. animator = Instance.new("Animator")
  26. animator.Parent = humanoid
  27. end
  28.  
  29. --------------------------------------------------
  30. -- ПОИСК АНИМАЦИИ ХОДЬБЫ
  31. --------------------------------------------------
  32.  
  33. local animateScript = pirate:FindFirstChild("Animate")
  34.  
  35. local walkAnimation = nil
  36.  
  37. if animateScript then
  38.  
  39. -- Основной вариант:
  40. -- Animate > walk > WalkAnim
  41. local walkFolder = animateScript:FindFirstChild("walk")
  42.  
  43. if walkFolder then
  44. walkAnimation = walkFolder:FindFirstChild("WalkAnim")
  45. end
  46.  
  47. -- Если не нашли WalkAnim, ищем любую Animation
  48. -- внутри объекта walk
  49. if not walkAnimation and walkFolder then
  50. for _, object in ipairs(walkFolder:GetDescendants()) do
  51. if object:IsA("Animation") then
  52. walkAnimation = object
  53. break
  54. end
  55. end
  56. end
  57.  
  58. -- Запасной вариант:
  59. -- Animate > run > RunAnim
  60. if not walkAnimation then
  61. local runFolder = animateScript:FindFirstChild("run")
  62.  
  63. if runFolder then
  64. walkAnimation = runFolder:FindFirstChild("RunAnim")
  65.  
  66. if not walkAnimation then
  67. for _, object in ipairs(runFolder:GetDescendants()) do
  68. if object:IsA("Animation") then
  69. walkAnimation = object
  70. break
  71. end
  72. end
  73. end
  74. end
  75. end
  76. end
  77.  
  78. --------------------------------------------------
  79. -- ЗАГРУЖАЕМ АНИМАЦИЮ
  80. --------------------------------------------------
  81.  
  82. local walkTrack = nil
  83.  
  84. if walkAnimation and walkAnimation:IsA("Animation") then
  85.  
  86. walkTrack = animator:LoadAnimation(walkAnimation)
  87.  
  88. walkTrack.Looped = true
  89. walkTrack.Priority = Enum.AnimationPriority.Movement
  90.  
  91. print("Анимация ходьбы найдена у", pirate.Name)
  92. print("AnimationId:", walkAnimation.AnimationId)
  93.  
  94. else
  95.  
  96. warn("У Pirate не найдена анимация WalkAnim!")
  97.  
  98. end
  99.  
  100. --------------------------------------------------
  101. -- ФУНКЦИИ ХОДЬБЫ
  102. --------------------------------------------------
  103.  
  104. local function startWalking()
  105.  
  106. if not walkTrack then
  107. return
  108. end
  109.  
  110. if not walkTrack.IsPlaying then
  111. walkTrack:Play(0.15)
  112. end
  113.  
  114. -- Стандартная скорость анимации рассчитана примерно на 16
  115. -- поэтому подстраиваем её под WalkSpeed.
  116. local animationSpeed = humanoid.WalkSpeed / 16
  117.  
  118. walkTrack:AdjustSpeed(math.max(animationSpeed, 0.1))
  119.  
  120. end
  121.  
  122.  
  123. local function stopWalking()
  124.  
  125. if walkTrack and walkTrack.IsPlaying then
  126. walkTrack:Stop(0.15)
  127. end
  128.  
  129. end
  130.  
  131. --------------------------------------------------
  132. -- НАСТРОЙКА СКОРОСТИ
  133. --------------------------------------------------
  134.  
  135. humanoid.WalkSpeed = walkSpeedValue.Value
  136.  
  137. walkSpeedValue.Changed:Connect(function()
  138.  
  139. if humanoid and humanoid.Parent then
  140. humanoid.WalkSpeed = walkSpeedValue.Value
  141. end
  142.  
  143. end)
  144.  
  145. --------------------------------------------------
  146. -- ПРЕСЛЕДОВАНИЕ
  147. --------------------------------------------------
  148.  
  149. while pirate.Parent do
  150.  
  151. if humanoid.Health <= 0 then
  152. stopWalking()
  153. break
  154. end
  155.  
  156. -- Пока атакует, не двигаемся
  157. if pirate:GetAttribute("IsAttacking") then
  158.  
  159. stopWalking()
  160. humanoid:MoveTo(rootPart.Position)
  161.  
  162. task.wait(0.1)
  163. continue
  164.  
  165. end
  166.  
  167. local player = playerValue.Value
  168.  
  169. if not player then
  170. stopWalking()
  171. task.wait(UPDATE_TIME)
  172. continue
  173. end
  174.  
  175. local character = player.Character
  176.  
  177. if not character then
  178. stopWalking()
  179. task.wait(UPDATE_TIME)
  180. continue
  181. end
  182.  
  183. local targetHumanoid = character:FindFirstChildOfClass("Humanoid")
  184. local targetRoot = character:FindFirstChild("HumanoidRootPart")
  185.  
  186. if not targetHumanoid or not targetRoot or targetHumanoid.Health <= 0 then
  187. stopWalking()
  188. task.wait(UPDATE_TIME)
  189. continue
  190. end
  191.  
  192. local distance = (
  193. targetRoot.Position - rootPart.Position
  194. ).Magnitude
  195.  
  196. --------------------------------------------------
  197. -- ДОШЛИ ДО ИГРОКА
  198. --------------------------------------------------
  199.  
  200. if distance <= ATTACK_DISTANCE then
  201.  
  202. stopWalking()
  203.  
  204. humanoid:MoveTo(rootPart.Position)
  205.  
  206. task.wait(0.1)
  207. continue
  208.  
  209. end
  210.  
  211. --------------------------------------------------
  212. -- ИДЁМ К ИГРОКУ
  213. --------------------------------------------------
  214.  
  215. startWalking()
  216.  
  217. local path = PathfindingService:CreatePath({
  218. AgentRadius = 2,
  219. AgentHeight = 5,
  220. AgentCanJump = true,
  221. WaypointSpacing = 3
  222. })
  223.  
  224. local success = pcall(function()
  225. path:ComputeAsync(
  226. rootPart.Position,
  227. targetRoot.Position
  228. )
  229. end)
  230.  
  231. if success and path.Status == Enum.PathStatus.Success then
  232.  
  233. local waypoints = path:GetWaypoints()
  234.  
  235. for i = 2, #waypoints do
  236.  
  237. if pirate:GetAttribute("IsAttacking") then
  238. break
  239. end
  240.  
  241. character = playerValue.Value.Character
  242.  
  243. if not character then
  244. break
  245. end
  246.  
  247. targetHumanoid = character:FindFirstChildOfClass("Humanoid")
  248. targetRoot = character:FindFirstChild("HumanoidRootPart")
  249.  
  250. if not targetHumanoid or not targetRoot then
  251. break
  252. end
  253.  
  254. if targetHumanoid.Health <= 0 then
  255. break
  256. end
  257.  
  258. distance = (
  259. targetRoot.Position - rootPart.Position
  260. ).Magnitude
  261.  
  262. -- Подошли достаточно близко
  263. if distance <= ATTACK_DISTANCE then
  264. break
  265. end
  266.  
  267. local waypoint = waypoints[i]
  268.  
  269. if waypoint.Action == Enum.PathWaypointAction.Jump then
  270. humanoid.Jump = true
  271. end
  272.  
  273. startWalking()
  274.  
  275. humanoid:MoveTo(waypoint.Position)
  276.  
  277. local reached = humanoid.MoveToFinished:Wait()
  278.  
  279. if not reached then
  280. break
  281. end
  282.  
  283. end
  284.  
  285. else
  286.  
  287. -- Если Pathfinding не смог построить путь,
  288. -- идём прямо к игроку
  289. startWalking()
  290.  
  291. humanoid:MoveTo(targetRoot.Position)
  292.  
  293. end
  294.  
  295. task.wait(UPDATE_TIME)
  296.  
  297. end
Advertisement
Add Comment
Please, Sign In to add comment