Advertisement
C-H-4-0-S

Coca-Cola Liet

Nov 15th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. local players = game:GetService("Players")
  2. local pathfindingService = game:GetService("PathfindingService")
  3. local userInputService = game:GetService("UserInputService")
  4. local camera = workspace.CurrentCamera
  5.  
  6. local player = players.LocalPlayer
  7. local character = player.Character or player.CharacterAdded:Wait()
  8. local humanoid = character:WaitForChild("Humanoid")
  9. local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
  10.  
  11. local Range = 10 -- Range for detecting parts in the viewport
  12. local followDistance = 5 -- Distance to maintain from the nearest player
  13. local isRunning = false -- Toggle state
  14. local path = nil -- Path object for pathfinding
  15.  
  16. -- Function to get the nearest player
  17. local function getNearestPlayer()
  18. local nearestPlayer = nil
  19. local nearestDistance = math.huge
  20.  
  21. for _, otherPlayer in ipairs(players:GetPlayers()) do
  22. if otherPlayer ~= player and otherPlayer.Character then
  23. local otherHRP = otherPlayer.Character:FindFirstChild("HumanoidRootPart")
  24. local otherHumanoid = otherPlayer.Character:FindFirstChild("Humanoid")
  25.  
  26. if otherHRP and otherHumanoid and otherHumanoid.Health > 0 then
  27. local distance = (humanoidRootPart.Position - otherHRP.Position).Magnitude
  28. if distance < nearestDistance then
  29. nearestDistance = distance
  30. nearestPlayer = otherPlayer
  31. end
  32. end
  33. end
  34. end
  35.  
  36. return nearestPlayer
  37. end
  38.  
  39. -- Function to use pathfinding to follow the nearest player
  40. local function followNearestPlayer()
  41. local targetPlayer = getNearestPlayer()
  42.  
  43. if targetPlayer and targetPlayer.Character then
  44. local targetHumanoid = targetPlayer.Character:FindFirstChild("Humanoid")
  45. local targetHRP = targetPlayer.Character:FindFirstChild("HumanoidRootPart")
  46.  
  47. if targetHumanoid and targetHumanoid.Health > 0 and targetHRP then
  48. local destination = targetHRP.Position - (targetHRP.CFrame.LookVector * followDistance)
  49.  
  50. -- Create a path to the target
  51. path = pathfindingService:CreatePath({
  52. AgentRadius = humanoid.HipHeight,
  53. AgentHeight = humanoid.HipHeight * 2,
  54. AgentCanJump = true,
  55. AgentJumpHeight = humanoid.JumpPower,
  56. AgentMaxSlope = humanoid.MaxSlopeAngle,
  57. })
  58.  
  59. path:ComputeAsync(humanoidRootPart.Position, destination)
  60.  
  61. -- Check if the path is completed
  62. if path.Status == Enum.PathStatus.Success then
  63. for _, waypoint in pairs(path:GetWaypoints()) do
  64. if not isRunning then break end -- Stop if toggle is disabled
  65.  
  66. if waypoint.Action == Enum.PathWaypointAction.Jump then
  67. humanoid.Jump = true
  68. end
  69. humanoid:MoveTo(waypoint.Position)
  70. humanoid.MoveToFinished:Wait()
  71. end
  72. else
  73. warn("Pathfinding failed.")
  74. end
  75. end
  76. end
  77. end
  78.  
  79. -- Function to get parts in the viewport within a certain range
  80. local function getPartsInViewport(maxDistance)
  81. local partsInViewport = {}
  82. for _, part in ipairs(workspace:GetDescendants()) do
  83. if part:IsA("BasePart") then
  84. local distance = player:DistanceFromCharacter(part.Position)
  85. if distance <= maxDistance then
  86. local _, isVisible = camera:WorldToViewportPoint(part.Position)
  87. if isVisible then
  88. table.insert(partsInViewport, part)
  89. end
  90. end
  91. end
  92. end
  93. return partsInViewport
  94. end
  95.  
  96. -- Function to activate tools on visible parts
  97. local function activateToolOnParts()
  98. local tool = player.Character and player.Character:FindFirstChildOfClass("Tool")
  99. local parts = getPartsInViewport(Range)
  100.  
  101. if tool and tool:FindFirstChild("Handle") then
  102. for _, part in ipairs(parts) do
  103. if part and part.Parent and part.Parent ~= player.Character and part.Parent:FindFirstChildWhichIsA("Humanoid") and part.Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
  104. tool:Activate()
  105. firetouchinterest(tool.Handle, part, 0)
  106. firetouchinterest(tool.Handle, part, 1)
  107. end
  108. end
  109. end
  110. end
  111.  
  112. -- Function to handle the combined execution logic
  113. local function combinedExecution()
  114. while isRunning do
  115. followNearestPlayer()
  116. activateToolOnParts()
  117. wait(0.1) -- Small delay to avoid overloading
  118. end
  119. end
  120.  
  121. -- Function to toggle the execution state
  122. local function toggleExecution()
  123. isRunning = not isRunning -- Toggle the state
  124.  
  125. if isRunning then
  126. print("Execution started.")
  127. combinedExecution()
  128. else
  129. print("Execution stopped.")
  130. if path then
  131. path:Destroy()
  132. end
  133. end
  134. end
  135.  
  136. -- Keybind for toggling execution
  137. userInputService.InputBegan:Connect(function(input, gameProcessed)
  138. if not gameProcessed and input.KeyCode == Enum.KeyCode.C then
  139. toggleExecution()
  140. end
  141. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement