Certainly! Below is the enhanced Roblox Lua script incorporating the requested features:
- Perfect Circle: Implements a dynamic circular movement system around each tracked target.
- Always Target the Torso: Ensures that the ESP indicators and tracking focus on the
HumanoidRootPart (torso) of each target.
- Selection/Deselection: Allows selecting and deselecting multiple targets using the
E key.
- Multiple Handlers: Manages multiple ESP indicators and targets efficiently.
- Instant Rotation: Utilizes a fast and smooth rotation system to face the primary target almost instantly.
Enhanced Lock-On System Script
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
-- Configuration
local ROTATION_SPEED = 0.15 -- Speed of the rotation interpolation (lower is faster)
local ESP_TEXT = "Targeted" -- Text displayed in ESP
local ESP_COLOR = Color3.fromRGB(255, 0, 0) -- Color of the ESP text
local MAX_LOCKED_TARGETS = 5 -- Maximum number of concurrent locked targets
local CIRCLE_RADIUS = 5 -- Radius for circular movement around targets
local CIRCLE_SPEED = 0.5 -- Speed of the circular movement
-- Variables
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local trackedTargets = {} -- Table to store currently tracked targets
local espIndicators = {} -- Table to store ESP BillboardGuis
-- Helper Function: Check if a model has a Humanoid
local function hasHumanoid(model)
return model and model:FindFirstChildOfClass("Humanoid") ~= nil
end
-- Helper Function: Find the closest player to the local player not already tracked
local function findClosestPlayer()
local closestPlayer = nil
local shortestDistance = math.huge
for _, otherPlayer in ipairs(Players:GetPlayers()) do
if otherPlayer ~= player and otherPlayer.Character and hasHumanoid(otherPlayer.Character) and otherPlayer.Character:FindFirstChild("HumanoidRootPart") then
local distance = (otherPlayer.Character.HumanoidRootPart.Position - humanoidRootPart.Position).Magnitude
if distance < shortestDistance and not trackedTargets[otherPlayer] then
shortestDistance = distance
closestPlayer = otherPlayer
end
end
end
return closestPlayer
end
-- Function: Add a target to the trackedTargets table and create ESP
local function addTarget(targetPlayer)
if #trackedTargets >= MAX_LOCKED_TARGETS then
warn("Maximum number of locked targets reached.")
return
end
local targetCharacter = targetPlayer.Character
if targetCharacter and targetCharacter:FindFirstChild("HumanoidRootPart") then
trackedTargets[targetPlayer] = {
Character = targetCharacter,
CircleAngle = 0, -- Starting angle for circular movement
}
-- Create ESP indicator
local espIndicator = Instance.new("BillboardGui")
espIndicator.Adornee = targetCharacter:FindFirstChild("HumanoidRootPart")
espIndicator.Size = UDim2.new(0, 100, 0, 50)
espIndicator.StudsOffset = Vector3.new(0, 2, 0)
espIndicator.AlwaysOnTop = true
espIndicator.Name = "TargetESP"
local textLabel = Instance.new("TextLabel")
textLabel.Parent = espIndicator
textLabel.Size = UDim2.new(1, 0, 1, 0)
textLabel.BackgroundTransparency = 1
textLabel.Text = ESP_TEXT
textLabel.TextColor3 = ESP_COLOR
textLabel.Font = Enum.Font.SourceSansBold
textLabel.TextScaled = true
textLabel.TextStrokeTransparency = 0.8
espIndicator.Parent = player:WaitForChild("PlayerGui")
espIndicators[targetPlayer] = espIndicator
end
end
-- Function: Remove a target from the trackedTargets table and destroy ESP
local function removeTarget(targetPlayer)
if trackedTargets[targetPlayer] then
trackedTargets[targetPlayer] = nil
end
if espIndicators[targetPlayer] then
espIndicators[targetPlayer]:Destroy()
espIndicators[targetPlayer] = nil
end
end
-- Function: Toggle Lock-On to the closest player when E is pressed
local function toggleLockOn()
local closestPlayer = findClosestPlayer()
if closestPlayer then
if trackedTargets[closestPlayer] then
-- Deselect the target
removeTarget(closestPlayer)
else
-- Select the target
addTarget(closestPlayer)
end
end
end
-- Function: Smoothly rotate to face the primary target (first in trackedTargets)
local function smoothRotateToPrimaryTarget()
local primaryTarget = nil
for targetPlayer, _ in pairs(trackedTargets) do
primaryTarget = targetPlayer
break
end
if primaryTarget and primaryTarget.Character and primaryTarget.Character:FindFirstChild("HumanoidRootPart") then
local targetPart = primaryTarget.Character.HumanoidRootPart
local targetPosition = Vector3.new(targetPart.Position.X, humanoidRootPart.Position.Y, targetPart.Position.Z)
local direction = (targetPosition - humanoidRootPart.Position).Unit
local desiredCFrame = CFrame.lookAt(humanoidRootPart.Position, targetPosition)
-- Instant-like rotation using Tween
TweenService:Create(
humanoidRootPart,
TweenInfo.new(ROTATION_SPEED, Enum.EasingStyle.Linear),
{ CFrame = desiredCFrame }
):Play()
end
end
-- Function: Update circular movement around each target
local function updateCircularMovement(deltaTime)
for _, data in pairs(trackedTargets) do
local targetHumanoid = data.Character:FindFirstChild("HumanoidRootPart")
if targetHumanoid then
data.CircleAngle = (data.CircleAngle + CIRCLE_SPEED * deltaTime) % (2 * math.pi)
local offset = Vector3.new(math.cos(data.CircleAngle), 0, math.sin(data.CircleAngle)) * CIRCLE_RADIUS
targetHumanoid.CFrame = targetHumanoid.CFrame * CFrame.new(offset)
end
end
end
-- Event Listener: Detect when E key is pressed
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.E then
toggleLockOn()
end
end)
-- Event Listener: Handle character respawn
player.CharacterAdded:Connect(function(newCharacter)
character = newCharacter
humanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- Reset tracking and ESP on respawn
for targetPlayer, _ in pairs(trackedTargets) do
removeTarget(targetPlayer)
end
end)
-- Monitor target character removal or death
Players.PlayerRemoving:Connect(function(leavingPlayer)
if trackedTargets[leavingPlayer] then
removeTarget(leavingPlayer)
end
end)
-- Update circular movement and rotation each frame
RunService.RenderStepped:Connect(function(deltaTime)
if next(trackedTargets) then
smoothRotateToPrimaryTarget()
updateCircularMovement(deltaTime)
end
end)
Key Enhancements Explained
-
Multiple Target Tracking:
trackedTargets Table: Stores all currently locked-on targets with additional data such as CircleAngle for circular movement.
- Maximum Targets: Configured via
MAX_LOCKED_TARGETS to limit the number of simultaneous targets.
- Selection Logic: Pressing the
E key toggles the closest target's lock-on state. If the maximum number of targets is reached, it warns and doesn't add more.
-
ESP Indicators for Multiple Targets:
- Each tracked target has its own
BillboardGui ESP indicator stored in the espIndicators table.
- ESP is focused on the
HumanoidRootPart (torso) to ensure consistency in targeting.
-
Instant and Smooth Rotation:
- Primary Target Rotation: The script identifies the first added target as the primary target and smoothly rotates the player's
HumanoidRootPart to face this target using TweenService for near-instant rotation.
- Tween Configuration:
ROTATION_SPEED determines how quickly the rotation occurs. A lower value means faster rotation.
-
Dynamic Circular Movement:
updateCircularMovement Function: Moves each target in a perfect circle around its current position, creating a dynamic environment. This can simulate targets dodging or adding motion to their positioning.
- Parameters:
CIRCLE_RADIUS and CIRCLE_SPEED control the size and speed of the circular movement.
-
Robust Target Management:
- Character Respawn Handling: When the local player respawns, all current lock-ons are cleared to prevent inconsistencies.
- Player Leaving Handling: If a locked target leaves the game, the script automatically removes it from the
trackedTargets table and destroys its ESP indicator.
-
Code Organization and Comments:
- Comprehensive comments are added for better readability and maintenance.
- Functions are modularized for clarity and reusability.
Usage Instructions
-
Locking Onto Targets:
- Press the
E key to lock onto the closest player. Pressing E again will unlock them.
- You can lock onto multiple players by pressing
E multiple times, up to the MAX_LOCKED_TARGETS limit.
-
Visual Indicators:
- Locked targets will display an ESP indicator (
"Targeted") above their torso for easy identification.
-
Dynamic Behavior:
- Locked targets will move in a perfect circle around their position, enhancing the dynamic nature of the tracking system.
- The player's character will rotate smoothly and almost instantly to face the primary target.
Customization
Conclusion
This enhanced script provides a robust and dynamic lock-on system tailored to your specifications. It efficiently manages multiple targets, ensures smooth and near-instant rotation, and maintains a dynamic circular movement around each target's torso. Feel free to further customize the script to better fit your game's unique requirements!