Advertisement
FlyingFrog

Преследующий монстр

Mar 15th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. local PathfindingService = game:GetService("PathfindingService")
  2. local Players = game:GetService("Players")
  3. local RunService = game:GetService("RunService")
  4.  
  5. local path = PathfindingService:CreatePath({
  6. AgentHeight = 12;
  7. AgentRadius = 6;
  8. AgentCanJump = false;
  9.  
  10. Costs = {
  11. Water = 100;
  12. DangerZone = math.huge
  13. }
  14. })
  15.  
  16. local Character = script.Parent
  17. local humanoid = Character:WaitForChild("Humanoid")
  18.  
  19. local waypoints
  20. local nextWaypointIndex
  21. local reachedConnection
  22. local blockedConnection
  23.  
  24. local function findTarget()
  25. local maxDistance = 500
  26. local nearestTarget
  27.  
  28. for index, player in pairs(Players:GetPlayers()) do
  29. if player.Character then
  30. local target = player.Character
  31. local distance = (Character.HumanoidRootPart.Position
  32. - target.HumanoidRootPart.Position).Magnitude
  33.  
  34. if distance < maxDistance and target.Humanoid.Health > 0 then
  35. nearestTarget = target
  36. maxDistance = distance
  37. end
  38.  
  39. if distance < 5 then
  40. nearestTarget.Humanoid:TakeDamage(25)
  41. end
  42. end
  43. end
  44. return nearestTarget
  45. end
  46.  
  47. local function FollowPath(destination)
  48. local success, errorMessage = pcall(function()
  49. path:ComputeAsync(Character.PrimaryPart.Position, destination)
  50. end)
  51.  
  52. if success and path.Status == Enum.PathStatus.Success then
  53. waypoints = path:GetWaypoints()
  54.  
  55. blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
  56. if blockedWaypointIndex >= nextWaypointIndex then
  57. blockedConnection:Disconnect()
  58. FollowPath(destination)
  59. end
  60. end)
  61. if not reachedConnection then
  62. reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
  63. if reached and nextWaypointIndex < #waypoints then
  64. nextWaypointIndex += 1
  65. humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
  66. else
  67. reachedConnection:Disconnect()
  68. blockedConnection:Disconnect()
  69. end
  70. end)
  71. end
  72.  
  73. nextWaypointIndex = 2
  74. humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
  75. else
  76. warn("Path not computed", errorMessage)
  77. end
  78. end
  79.  
  80. while wait() do
  81. local target = findTarget()
  82. if target then
  83. print(target.Name)
  84. FollowPath(target.HumanoidRootPart.Position)
  85. end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement