Hasli4

Untitled

Oct 4th, 2026
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 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.5
  15.  
  16. pirate:SetAttribute("IsAttacking", false)
  17.  
  18. while pirate.Parent do
  19.  
  20. humanoid.WalkSpeed = walkSpeedValue.Value
  21.  
  22. -- Если пират сейчас атакует, движение останавливаем
  23. if pirate:GetAttribute("IsAttacking") then
  24. humanoid:MoveTo(rootPart.Position)
  25. task.wait(0.1)
  26. continue
  27. end
  28.  
  29. local player = playerValue.Value
  30.  
  31. if not player then
  32. task.wait(UPDATE_TIME)
  33. continue
  34. end
  35.  
  36. local character = player.Character
  37.  
  38. if not character then
  39. task.wait(UPDATE_TIME)
  40. continue
  41. end
  42.  
  43. local targetHumanoid = character:FindFirstChildOfClass("Humanoid")
  44. local targetRoot = character:FindFirstChild("HumanoidRootPart")
  45.  
  46. if not targetHumanoid or not targetRoot or targetHumanoid.Health <= 0 then
  47. task.wait(UPDATE_TIME)
  48. continue
  49. end
  50.  
  51. local distance = (targetRoot.Position - rootPart.Position).Magnitude
  52.  
  53. -- Если уже достаточно близко, стоим на месте.
  54. -- Дальше атакой займется PirateAttack.
  55. if distance <= ATTACK_DISTANCE then
  56. humanoid:MoveTo(rootPart.Position)
  57. task.wait(0.1)
  58. continue
  59. end
  60.  
  61. -- Создаем путь к игроку
  62. local path = PathfindingService:CreatePath({
  63. AgentRadius = 2,
  64. AgentHeight = 5,
  65. AgentCanJump = true,
  66. WaypointSpacing = 3
  67. })
  68.  
  69. local success = pcall(function()
  70. path:ComputeAsync(rootPart.Position, targetRoot.Position)
  71. end)
  72.  
  73. if success and path.Status == Enum.PathStatus.Success then
  74.  
  75. local waypoints = path:GetWaypoints()
  76.  
  77. for _, waypoint in ipairs(waypoints) do
  78.  
  79. if pirate:GetAttribute("IsAttacking") then
  80. break
  81. end
  82.  
  83. if not playerValue.Value then
  84. break
  85. end
  86.  
  87. character = playerValue.Value.Character
  88.  
  89. if not character then
  90. break
  91. end
  92.  
  93. targetRoot = character:FindFirstChild("HumanoidRootPart")
  94. targetHumanoid = character:FindFirstChildOfClass("Humanoid")
  95.  
  96. if not targetRoot or not targetHumanoid or targetHumanoid.Health <= 0 then
  97. break
  98. end
  99.  
  100. distance = (targetRoot.Position - rootPart.Position).Magnitude
  101.  
  102. if distance <= ATTACK_DISTANCE then
  103. break
  104. end
  105.  
  106. if waypoint.Action == Enum.PathWaypointAction.Jump then
  107. humanoid.Jump = true
  108. end
  109.  
  110. humanoid:MoveTo(waypoint.Position)
  111.  
  112. local reached = humanoid.MoveToFinished:Wait()
  113.  
  114. if not reached then
  115. break
  116. end
  117. end
  118.  
  119. else
  120. -- Если путь построить не получилось,
  121. -- просто пытаемся идти прямо к игроку.
  122. humanoid:MoveTo(targetRoot.Position)
  123. end
  124.  
  125. task.wait(UPDATE_TIME)
  126. end
  127.  
Advertisement
Add Comment
Please, Sign In to add comment