-- Aim Lock Script for Roblox (LocalScript)
-- Press X to toggle aim lock onto the player under your mouse.
-- When locked, the camera smoothly aims at the part under the mouse (falls back to HumanoidRootPart).
-- Place this LocalScript in StarterPlayerScripts.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
-- Settings
local AimLockKey = Enum.KeyCode.X -- changed to X
local AimSensitivity = 0.12 -- how fast the camera moves toward the target each frame (0..1)
-- 0.0 = no movement, 1.0 = snap instantly. Tweak to taste.
local aimLockEnabled = false
local targetPlayer = nil
local lockedPart = nil -- the specific part under mouse when locking (may be head/torso/limb)
local mouse = LocalPlayer:GetMouse()
-- Helper: get the HumanoidRootPart of a character
local function getMiddlePart(character)
if not character then return nil end
return character:FindFirstChild("HumanoidRootPart")
end
-- Helper: find player from a BasePart (by locating the ancestor Model and checking Players)
local function getPlayerFromPart(part)
if not part or not part:IsA("BasePart") then return nil end
local model = part:FindFirstAncestorOfClass("Model")
if not model then return nil end
return Players:GetPlayerFromCharacter(model)
end
-- Get the player currently under the mouse (if any)
local function getPlayerUnderMouse()
local targetPart = mouse.Target
if not targetPart then
return nil, nil
end
local player = getPlayerFromPart(targetPart)
return player, targetPart
end
-- Attempt to lock onto the player under the mouse right now
local function lockOntoMouseTarget()
local player, part = getPlayerUnderMouse()
if player and player.Character then
targetPlayer = player
lockedPart = part
aimLockEnabled = true
print("Aim Lock Enabled -> Locked to player:", player.Name)
else
-- No player under mouse at this moment
targetPlayer = nil
lockedPart = nil
aimLockEnabled = false
print("No player under mouse to lock onto.")
end
end
-- Clear lock
local function clearLock()
aimLockEnabled = false
targetPlayer = nil
lockedPart = nil
print("Aim Lock Disabled")
end
-- Toggle lock when X is pressed
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == AimLockKey then
if aimLockEnabled then
clearLock()
else
lockOntoMouseTarget()
end
end
end)
-- Main loop: smoothly aim camera at the locked target (updates every frame)
RunService.RenderStepped:Connect(function()
if not aimLockEnabled or not targetPlayer then
return
end
-- Ensure the player and character still exist
if not targetPlayer.Parent or not targetPlayer.Character then
clearLock()
return
end
-- Prefer the originally locked part (so if mouse was over head, we keep aiming head).
-- If that part is gone, fallback to HumanoidRootPart.
local aimPart = nil
if lockedPart and lockedPart:IsDescendantOf(targetPlayer.Character) then
aimPart = lockedPart
else
aimPart = getMiddlePart(targetPlayer.Character)
end
if not aimPart then
clearLock()
return
end
-- Compute desired camera CFrame looking at the aim position
local aimPosition = aimPart.Position
local camPos = Camera.CFrame.Position
local desiredCFrame = CFrame.new(camPos, aimPosition)
-- Smoothly interpolate camera CFrame towards desired look-at
local t = math.clamp(AimSensitivity, 0, 1)
Camera.CFrame = Camera.CFrame:Lerp(desiredCFrame, t)
end)
-- Optional: allow retargeting while aim lock is enabled by pressing X again to re-lock to current mouse target.
-- The current implementation toggles on/off with X; if you want immediate re-locking while enabled, you can
-- modify the InputBegan handler to call lockOntoMouseTarget() unconditionally.
make this script better by toggling the activation button "X" Instead of double tapping and increase the max distance by 3000 studs and make it be abble to be usable through walls aswell