Advertisement
A_Xcom

Roblox Aimlock script

Mar 2nd, 2025
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | Source Code | 0 0
  1. -- Aimlock Script for Roblox
  2. -- Features:
  3. -- - Locks onto nearest player to crosshair
  4. -- - Only locks onto players from other teams
  5. -- - Locks to head with zero smoothness (instant)
  6. -- - Toggle with E key
  7. -- - Only locks onto one player at a time
  8.  
  9. local UserInputService = game:GetService("UserInputService")
  10. local RunService = game:GetService("RunService")
  11. local Players = game:GetService("Players")
  12. local Camera = workspace.CurrentCamera
  13.  
  14. local LocalPlayer = Players.LocalPlayer
  15. local Mouse = LocalPlayer:GetMouse()
  16.  
  17. -- Settings
  18. local ENABLED = false
  19. local KEY_TO_TOGGLE = Enum.KeyCode.E -- Changed to E key
  20. local MAX_DISTANCE = 1000
  21. local FOV = 300 -- Field of View for target selection
  22. local lockedPlayer = nil -- Variable to store the locked player
  23.  
  24. -- Function to check if a player is on a different team
  25. local function isPlayerOnDifferentTeam(player)
  26.     return player.Team ~= LocalPlayer.Team
  27. end
  28.  
  29. -- Function to check if a player is visible
  30. local function isPlayerVisible(player)
  31.     local character = player.Character
  32.     if not character then return false end
  33.    
  34.     local head = character:FindFirstChild("Head")
  35.     if not head then return false end
  36.    
  37.     local ray = Ray.new(Camera.CFrame.Position, (head.Position - Camera.CFrame.Position).Unit * MAX_DISTANCE)
  38.     local hitPart, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, {LocalPlayer.Character})
  39.    
  40.     return hitPart and hitPart:IsDescendantOf(character)
  41. end
  42.  
  43. -- Function to get the nearest player to the crosshair
  44. local function getNearestPlayerToCrosshair()
  45.     local nearestPlayer = nil
  46.     local shortestDistance = FOV
  47.    
  48.     for _, player in pairs(Players:GetPlayers()) do
  49.         if player ~= LocalPlayer and player.Character and player.Character:FindFirstChild("Head") and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health > 0 then
  50.             -- Check if player is on different team
  51.             if isPlayerOnDifferentTeam(player) then
  52.                 local screenPoint = Camera:WorldToScreenPoint(player.Character.Head.Position)
  53.                 local screenDistance = (Vector2.new(screenPoint.X, screenPoint.Y) - Vector2.new(Mouse.X, Mouse.Y)).Magnitude
  54.                
  55.                 if screenDistance < shortestDistance then
  56.                     shortestDistance = screenDistance
  57.                     nearestPlayer = player
  58.                 end
  59.             end
  60.         end
  61.     end
  62.    
  63.     return nearestPlayer
  64. end
  65.  
  66. -- Toggle function
  67. UserInputService.InputBegan:Connect(function(input, gameProcessed)
  68.     if not gameProcessed and input.KeyCode == KEY_TO_TOGGLE then
  69.         ENABLED = not ENABLED
  70.        
  71.         if ENABLED then
  72.             -- Find a target when enabling
  73.             lockedPlayer = getNearestPlayerToCrosshair()
  74.         else
  75.             -- Clear target when disabling
  76.             lockedPlayer = nil
  77.         end
  78.     end
  79. end)
  80.  
  81. -- Main aimlock loop
  82. RunService.RenderStepped:Connect(function()
  83.     if ENABLED and lockedPlayer then
  84.         -- Check if locked player is still valid
  85.         if lockedPlayer.Character and
  86.            lockedPlayer.Character:FindFirstChild("Head") and
  87.            lockedPlayer.Character:FindFirstChild("Humanoid") and
  88.            lockedPlayer.Character.Humanoid.Health > 0 and
  89.            isPlayerOnDifferentTeam(lockedPlayer) then
  90.            
  91.             -- Instant lock to head (0 smoothness)
  92.             Camera.CFrame = CFrame.new(Camera.CFrame.Position, lockedPlayer.Character.Head.Position)
  93.         else
  94.             -- If target is no longer valid, find a new one
  95.             lockedPlayer = getNearestPlayerToCrosshair()
  96.         end
  97.     end
  98. end)
  99.  
  100. -- Notification
  101. local function createNotification()
  102.     local screenGui = Instance.new("ScreenGui")
  103.     screenGui.Parent = game.CoreGui
  104.    
  105.     local textLabel = Instance.new("TextLabel")
  106.     textLabel.Size = UDim2.new(0, 200, 0, 50)
  107.     textLabel.Position = UDim2.new(0.5, -100, 0, 30)
  108.     textLabel.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
  109.     textLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
  110.     textLabel.Text = "Aimlock Loaded\nToggle with E key"
  111.     textLabel.Parent = screenGui
  112.    
  113.     game:GetService("Debris"):AddItem(screenGui, 3)
  114. end
  115.  
  116. createNotification()
  117.  
  118. print("Aimlock script loaded. Press E to toggle.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement