Advertisement
aesnike

FIXING

Oct 25th, 2024 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local LocalPlayer = Players.LocalPlayer
  3. local Character
  4. local CurrentTarget
  5.  
  6. local teleportDelay = 2 -- Delay in seconds before teleporting to a new target
  7. local lastTeleportTime = 0 -- Tracks the last teleport time
  8. local VirtualInputManager = game:GetService("VirtualInputManager") -- Assuming this is defined elsewhere
  9.  
  10. local function teleportToNearestNPC()
  11. local nearestNPC = nil
  12. local nearestDistance = math.huge
  13.  
  14. Character = LocalPlayer.Character
  15. if not Character or not Character:FindFirstChild("HumanoidRootPart") then return end
  16.  
  17. -- Find nearest NPC
  18. for _, folder in ipairs(workspace:GetChildren()) do
  19. if folder:IsA("Folder") then
  20. for _, child in ipairs(folder:GetChildren()) do
  21. if child:FindFirstChild("enemyFolder") then
  22. for _, npc in ipairs(child.enemyFolder:GetChildren()) do
  23. if npc:FindFirstChild("HumanoidRootPart") and npc:FindFirstChild("Humanoid") and npc.Humanoid.Health > 0 then
  24. local distance = (Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
  25. if distance < nearestDistance then
  26. nearestDistance = distance
  27. nearestNPC = npc
  28. end
  29. end
  30. end
  31. end
  32. end
  33. end
  34. end
  35.  
  36. if nearestNPC then
  37. CurrentTarget = nearestNPC -- Store the current target
  38. lastTeleportTime = tick() -- Reset the last teleport time
  39. end
  40. end
  41.  
  42. local function pressKey(key)
  43. VirtualInputManager:SendKeyEvent(true, key, false, nil)
  44. wait() -- Small delay between press and release
  45. VirtualInputManager:SendKeyEvent(false, key, false, nil)
  46. end
  47.  
  48. game:GetService("RunService").Heartbeat:Connect(function()
  49. if CurrentTarget and CurrentTarget:FindFirstChild("HumanoidRootPart") and CurrentTarget.Humanoid.Health > 0 then
  50. if Character and Character:FindFirstChild("HumanoidRootPart") then
  51. -- Check if the character is grounded
  52. if Character.Humanoid:GetState() == Enum.HumanoidStateType.Freefall then return end
  53.  
  54. -- Check if enough time has passed to teleport
  55. local currentTime = tick()
  56. if currentTime - lastTeleportTime >= teleportDelay then
  57. -- Ensure the character is close enough before teleporting
  58. local distanceToTarget = (Character.HumanoidRootPart.Position - CurrentTarget.HumanoidRootPart.Position).Magnitude
  59. if distanceToTarget <= 50 then -- Adjust this distance as needed
  60. -- Teleport to the target position directly above the NPC
  61. Character.HumanoidRootPart.CFrame = CFrame.new(CurrentTarget.HumanoidRootPart.Position + Vector3.new(0, 2, 0))
  62. lastTeleportTime = currentTime -- Update the last teleport time
  63. else
  64. -- Move closer to the target without teleporting
  65. Character.HumanoidRootPart.Position = Character.HumanoidRootPart.Position:Lerp(CurrentTarget.HumanoidRootPart.Position + Vector3.new(0, 2, 0), 0.1) -- Smooth movement
  66. end
  67. else
  68. -- Position the character above the target without teleporting
  69. Character.HumanoidRootPart.Position = CurrentTarget.HumanoidRootPart.Position + Vector3.new(0, 2, 0)
  70. end
  71.  
  72. -- Press keys if near mobs
  73. if (Character.HumanoidRootPart.Position - CurrentTarget.HumanoidRootPart.Position).Magnitude <= 20 then
  74. pressKey("E") -- Use virtual key presses
  75. pressKey("Q")
  76. end
  77. end
  78. else
  79. CurrentTarget = nil -- Clear target if it's dead or gone
  80. teleportToNearestNPC() -- Find a new target
  81. end
  82. end)
  83.  
  84. -- Handle character respawn
  85. LocalPlayer.CharacterAdded:Connect(function(newCharacter)
  86. Character = newCharacter
  87. wait(1)
  88. end)
  89.  
  90. -- Initial setup
  91. teleportToNearestNPC()
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement