Certainly! Below is a Roblox Lua script that allows players to lock on to the nearest humanoid by pressing the E key. When E is pressed, the script finds the closest humanoid character to the player, locks onto it by smoothly rotating the player's HumanoidRootPart to face the target, and displays an ESP (Extra Sensory Perception) indicator above the target's head. Pressing E again will deselect the current target.
Complete Script
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")
-- Configuration
local ROTATION_SPEED = 0.1 -- Smooth rotation speed
local LOCK_KEY = Enum.KeyCode.E -- Key to press for lock-on
local TRACKED_MODEL = nil -- Currently tracked model
local ESP_BILLBOARD = nil -- Reference to the ESP BillboardGui
local MAX_LOCK_DISTANCE = 100 -- Maximum distance to search for targets
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- Function to handle selecting/deselecting a target
local function toggleTarget(target)
if TRACKED_MODEL == target then
-- Deselect if the same target is selected again
TRACKED_MODEL = nil
if ESP_BILLBOARD then
ESP_BILLBOARD:Destroy()
ESP_BILLBOARD = nil
end
else
-- Select the new target
TRACKED_MODEL = target
-- Remove existing ESP if any
if ESP_BILLBOARD then
ESP_BILLBOARD:Destroy()
ESP_BILLBOARD = nil
end
-- Create ESP for the newly selected target
local targetHead = target:FindFirstChild("Head")
if targetHead then
ESP_BILLBOARD = Instance.new("BillboardGui")
ESP_BILLBOARD.Adornee = targetHead
ESP_BILLBOARD.Size = UDim2.new(0, 100, 0, 50)
ESP_BILLBOARD.StudsOffset = Vector3.new(0, 2, 0)
ESP_BILLBOARD.AlwaysOnTop = true
ESP_BILLBOARD.Name = "TargetESP"
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(1, 0, 1, 0)
textLabel.Text = "Targeted"
textLabel.TextColor3 = Color3.fromRGB(255, 0, 0)
textLabel.BackgroundTransparency = 1
textLabel.TextStrokeTransparency = 0.8
textLabel.Font = Enum.Font.SourceSansBold
textLabel.TextScaled = true
textLabel.Parent = ESP_BILLBOARD
ESP_BILLBOARD.Parent = player:WaitForChild("PlayerGui")
end
end
end
-- Function to find the nearest humanoid
local function findNearestHumanoid()
local closestDistance = MAX_LOCK_DISTANCE
local nearestHumanoid = nil
for _, otherPlayer in ipairs(Players:GetPlayers()) do
if otherPlayer ~= player and otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart") and otherPlayer.Character:FindFirstChild("Humanoid") then
local distance = (humanoidRootPart.Position - otherPlayer.Character.HumanoidRootPart.Position).Magnitude
if distance < closestDistance then
closestDistance = distance
nearestHumanoid = otherPlayer.Character
end
end
end
return nearestHumanoid
end
-- Function to smoothly face the target's direction
local function smoothFaceTarget(target)
if target and target:FindFirstChild("HumanoidRootPart") then
local targetHumanoidRootPart = target.HumanoidRootPart
local targetPosition = Vector3.new(
targetHumanoidRootPart.Position.X,
humanoidRootPart.Position.Y,
targetHumanoidRootPart.Position.Z
)
local targetCFrame = CFrame.lookAt(humanoidRootPart.Position, targetPosition)
-- Smoothly interpolate the humanoid's CFrame towards the target
humanoidRootPart.CFrame = humanoidRootPart.CFrame:Lerp(targetCFrame, ROTATION_SPEED)
end
end
-- Function to handle key press for lock-on
local function onKeyPressed(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == LOCK_KEY then
if TRACKED_MODEL then
-- If already tracking a target, deselect it
toggleTarget(TRACKED_MODEL)
else
-- Find and select the nearest humanoid
local nearest = findNearestHumanoid()
if nearest then
toggleTarget(nearest)
else
-- Optionally, you can notify the player that no target was found
-- For example:
-- player:FindFirstChildOfClass("PlayerGui"):SetCore("SendNotification", {
-- Title = "Lock-On",
-- Text = "No targets nearby!",
-- Duration = 2
-- })
end
end
end
end
-- Listen for key presses
UserInputService.InputBegan:Connect(onKeyPressed)
-- Ensure the script continues working when the character respawns
player.CharacterAdded:Connect(function(newCharacter)
character = newCharacter
humanoidRootPart = newCharacter:WaitForChild("HumanoidRootPart")
-- Clean up any existing ESP when the character respawns
if ESP_BILLBOARD then
ESP_BILLBOARD:Destroy()
ESP_BILLBOARD = nil
end
TRACKED_MODEL = nil
end)
-- RunService to update the face tracking smoothly
RunService.RenderStepped:Connect(function()
if TRACKED_MODEL then
smoothFaceTarget(TRACKED_MODEL)
end
end)
Explanation of the Script
-
Services and Variables Initialization
- Services: The script utilizes various Roblox services such as
Players, RunService, UserInputService, and Workspace.
- Configuration Variables:
ROTATION_SPEED: Determines how smoothly the player rotates to face the target.
LOCK_KEY: The keybind for activating the lock-on feature (E by default).
MAX_LOCK_DISTANCE: Maximum distance within which the script will search for the nearest humanoid.
-
toggleTarget Function
- Purpose: Selects or deselects a target. If the target is already selected, it will be deselected. Otherwise, it will lock on to the new target and create an ESP indicator.
- ESP Creation: A
BillboardGui with a TextLabel is created above the target's head to indicate that it's being targeted.
-
findNearestHumanoid Function
- Purpose: Iterates through all players in the game, excluding the local player, and finds the nearest player within the
MAX_LOCK_DISTANCE who has a HumanoidRootPart and Humanoid.
- Returns: The nearest humanoid character or
nil if no suitable target is found.
-
smoothFaceTarget Function
- Purpose: Smoothly rotates the player's
HumanoidRootPart to face the target's position using linear interpolation (Lerp) for a smooth transition.
-
onKeyPressed Function
- Purpose: Handles input from the player. When the
E key is pressed, it either locks onto the nearest target or deselects the current target if already locked on.
- Notification: There's a commented-out section where you can add a notification to inform the player if no targets are nearby.
-
Event Connections
InputBegan: Connects the onKeyPressed function to detect when the key is pressed.
Optional Enhancements
-
Visual Feedback for No Targets: You can uncomment and customize the notification section to provide feedback when no targets are available.
-
Customization of ESP: Modify the BillboardGui and TextLabel properties to change the appearance of the ESP indicator (e.g., color, size, text).
-
Adjustable Lock Distance: You can make MAX_LOCK_DISTANCE configurable through a GUI or settings panel to allow players to set their preferred lock-on range.
-
Multiple Targets: Extend the script to handle multiple lock-ons if desired, by managing a list of TRACKED_MODELs instead of a single one.
Usage Instructions
-
Script Placement:
- Insert the script into a LocalScript under
StarterPlayerScripts or another appropriate client-side location.
-
Testing:
- Play the game and press the
E key to lock onto the nearest humanoid within the specified range.
- Press
E again to deselect the current target.
-
Customization:
- Adjust
ROTATION_SPEED, MAX_LOCK_DISTANCE, and LOCK_KEY as needed to fit your game's requirements.
Important Considerations
-
Server vs. Client: This script is designed to run on the client side. Ensure it's placed in a LocalScript to function correctly.
-
Performance: Continuously searching for the nearest humanoid can impact performance in games with many players. Ensure MAX_LOCK_DISTANCE is optimized and consider optimizing the search algorithm if necessary.
-
Security: While this script handles client-side visuals (like ESP), always ensure that any critical game logic or security checks are handled server-side to prevent exploitation.
Feel free to modify and extend this script to better fit the specific needs of your game!