Fix this script and add in advanced handlers and debugging. ` -- Services local Players…
- User
Fix this script and add in advanced handlers and debugging.
code - Assistant
Fix this script and add in advanced handlers and debugging.
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
-- Configuration
local ROTATION_SPEED = 1.1 -- Speed of the rotation interpolation
local ESP_TEXT_PREFIX = "Targeted:" -- Prefix Text displayed in ESP
local ESP_COLOR = Color3.fromRGB(255, 0, 0) -- Color of the ESP text
local GUI_TOGGLE_KEY = Enum.KeyCode.T -- Key to toggle the GUI visibility
-- Variables
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local trackedTarget = nil -- Currently tracked target (another player)
local espIndicator = nil -- BillboardGui for ESP
local selectedPart = "HumanoidRootPart" -- Default targeted part
local guiVisible = true
-- List of possible target parts
local targetPartsListR6 = {
"Left Arm",
"Right Arm",
"Torso",
"Head",
"Left Leg",
"Right Leg"
}
local targetPartsListR15 = {
"LeftUpperArm",
"RightUpperArm",
"UpperTorso",
"Head",
"LeftUpperLeg",
"RightUpperLeg"
}
-- Determine if the character is R6 or R15 using RigType property
local isR15 = humanoid.RigType == Enum.HumanoidRigType.R15
local targetPartsList = isR15 and targetPartsListR15 or targetPartsListR6
-- 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
local function findClosestPlayer()
local closestPlayer = nil
local shortestDistance = math.huge
for _, otherPlayer in ipairs(Players:GetPlayers()) do
if otherPlayer ~= player and otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart") then
local distance = (otherPlayer.Character.HumanoidRootPart.Position - humanoidRootPart.Position).Magnitude
if distance < shortestDistance then
shortestDistance = distance
closestPlayer = otherPlayer
end
end
end
return closestPlayer
end
-- Function: Toggle Lock-On to the Closest Player when E is pressed
local function toggleLockOn()
if trackedTarget then
-- Deselect the current target
trackedTarget = nil
if espIndicator then
espIndicator:Destroy()
espIndicator = nil
end
else
-- Find and select the closest player
local closestPlayer = findClosestPlayer()
if closestPlayer and closestPlayer.Character then
trackedTarget = closestPlayer.Character
-- Create ESP indicator
local targetPartName = selectedPart
local targetPart = trackedTarget:FindFirstChild(targetPartName)
if not targetPart and isR15 then
-- For R15, parts might have different naming conventions
targetPart = trackedTarget:FindFirstChild(targetPartName)
elseif not targetPart then
-- For R6, some parts might have different names
targetPart = trackedTarget:FindFirstChild(targetPartName)
end
if targetPart then
-- Ensure previous ESP is removed
if espIndicator then
espIndicator:Destroy()
espIndicator = nil
end
-- Create BillboardGui for ESP
espIndicator = Instance.new("BillboardGui")
espIndicator.Adornee = targetPart
espIndicator.Size = UDim2.new(0, 100, 0, 50)
espIndicator.StudsOffset = Vector3.new(0, 2, 0)
espIndicator.AlwaysOnTop = true
espIndicator.Name = "TargetESP"
-- Create TextLabel for ESP
local textLabel = Instance.new("TextLabel")
textLabel.Parent = espIndicator
textLabel.Size = UDim2.new(1, 0, 1, 0)
textLabel.BackgroundTransparency = 1
textLabel.Text = ESP_TEXT_PREFIX .. " " .. targetPartName
textLabel.TextColor3 = ESP_COLOR
textLabel.Font = Enum.Font.SourceSansBold
textLabel.TextScaled = true
textLabel.TextStrokeTransparency = 0.8
-- Parent the ESP to the player's PlayerGui
espIndicator.Parent = player:WaitForChild("PlayerGui")
end
end
end
end
-- Function: Smoothly rotate to face the tracked target
local function smoothRotateToTarget()
if trackedTarget then
local targetPart = trackedTarget:FindFirstChild(selectedPart)
if targetPart then
local targetPosition = Vector3.new(targetPart.Position.X, humanoidRootPart.Position.Y, targetPart.Position.Z)
local direction = (targetPosition - humanoidRootPart.Position).Unit
local desiredCFrame = CFrame.new(humanoidRootPart.Position, targetPosition)
-- Advanced Lerp for smooth 360° rotation
humanoidRootPart.CFrame = humanoidRootPart.CFrame:Lerp(desiredCFrame, ROTATION_SPEED * RunService.RenderStepped:Wait())
end
end
end
-- Function: Create Draggable and Hiddable GUI
local function createTargetSelectionGUI()
-- Create ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "TargetSelectionGUI"
screenGui.Parent = player:WaitForChild("PlayerGui")
screenGui.ResetOnSpawn = false
-- Create Main Frame
local mainFrame = Instance.new("Frame")
mainFrame.Name = "MainFrame"
mainFrame.Size = UDim2.new(0, 200, 0, 150)
mainFrame.Position = UDim2.new(0.5, -100, 0.1, 0)
mainFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
mainFrame.BorderSizePixel = 0
mainFrame.Active = true
mainFrame.Draggable = true -- Makes the frame draggable
mainFrame.Parent = screenGui
-- Create Title Bar
local titleBar = Instance.new("TextLabel")
titleBar.Name = "TitleBar"
titleBar.Size = UDim2.new(1, 0, 0, 30)
titleBar.Position = UDim2.new(0, 0, 0, 0)
titleBar.BackgroundColor3 = Color3.fromRGB(45, 45, 45)
titleBar.BorderSizePixel = 0
titleBar.Text = "Target Selection"
titleBar.TextColor3 = Color3.fromRGB(255, 255, 255)
titleBar.TextScaled = true
titleBar.Font = Enum.Font.SourceSansBold
titleBar.Parent = mainFrame
-- Create Dropdown for Target Parts
local dropdown = Instance.new("Frame")
dropdown.Name = "Dropdown"
dropdown.Size = UDim2.new(0, 180, 0, 30)
dropdown.Position = UDim2.new(0, 10, 0, 40)
dropdown.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
dropdown.Parent = mainFrame
local dropdownLabel = Instance.new("TextLabel")
dropdownLabel.Name = "DropdownLabel"
dropdownLabel.Size = UDim2.new(1, 0, 1, 0)
dropdownLabel.Position = UDim2.new(0, 0, 0, 0)
dropdownLabel.BackgroundTransparency = 1
dropdownLabel.Text = "Select Part"
dropdownLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
dropdownLabel.TextScaled = true
dropdownLabel.Font = Enum.Font.SourceSans
dropdownLabel.Parent = dropdown
local dropdownButton = Instance.new("TextButton")
dropdownButton.Name = "DropdownButton"
dropdownButton.Size = UDim2.new(0, 20, 1, 0)
dropdownButton.Position = UDim2.new(1, -20, 0, 0)
dropdownButton.BackgroundTransparency = 1
dropdownButton.Text = "v"
dropdownButton.TextColor3 = Color3.fromRGB(255, 255, 255)
dropdownButton.Font = Enum.Font.SourceSans
dropdownButton.TextScaled = true
dropdownButton.Parent = dropdown
local dropdownList = Instance.new("Frame")
dropdownList.Name = "DropdownList"
dropdownList.Size = UDim2.new(0, 180, 0, 0)
dropdownList.Position = UDim2.new(0, 0, 0, 30)
dropdownList.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
dropdownList.Visible = false
dropdownList.ClipsDescendants = true
dropdownList.Parent = dropdown
local layout = Instance.new("UIListLayout")
layout.HorizontalAlignment = Enum.HorizontalAlignment.Left
layout.Parent = dropdownList
-- Populate Dropdown List
for _, partName in ipairs(targetPartsList) do
local option = Instance.new("TextButton")
option.Name = partName
option.Size = UDim2.new(1, 0, 0, 30)
option.BackgroundTransparency = 1
option.Text = partName
option.TextColor3 = Color3.fromRGB(255, 255, 255)
option.TextScaled = true
option.Font = Enum.Font.SourceSans
option.Parent = dropdownList
option.MouseButton1Click:Connect(function()
selectedPart = partName
dropdownLabel.Text = partName
dropdownList.Visible = false
end)
end
-- Toggle Dropdown List Visibility
dropdownButton.MouseButton1Click:Connect(function()
dropdownList.Visible = not dropdownList.Visible
end)
-- Close Dropdown if clicking outside using GuiService
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousePos = input.Position
local guiObjects = GuiService:GetGuiObjectsAtPosition(mousePos.X, mousePos.Y)
local clickedInsideDropdown = false
for _, obj in ipairs(guiObjects) do
if dropdown:IsAncestorOf(obj) then
clickedInsideDropdown = true
break
end
end
if not clickedInsideDropdown then
dropdownList.Visible = false
end
end
end
-- Connect the InputBegan event
UserInputService.InputBegan:Connect(onInputBegan)
end
-- Function: Toggle GUI Visibility
local function toggleGUIVisibility()
guiVisible = not guiVisible
local gui = player:WaitForChild("PlayerGui"):FindFirstChild("TargetSelectionGUI")
if gui then
gui.Enabled = guiVisible
end
end
-- Event Listeners
-- Toggle Lock-On and GUI Visibility based on key presses
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.E then
toggleLockOn()
elseif input.KeyCode == GUI_TOGGLE_KEY then
toggleGUIVisibility()
end
end)
-- Handle character respawn
player.CharacterAdded:Connect(function(newCharacter)
character = newCharacter
humanoid = character:WaitForChild("Humanoid")
humanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- Determine rig type using RigType property
isR15 = humanoid.RigType == Enum.HumanoidRigType.R15
targetPartsList = isR15 and targetPartsListR15 or targetPartsListR6
-- Reset tracking and ESP on respawn
trackedTarget = nil
if espIndicator then
espIndicator:Destroy()
espIndicator = nil
end
end)
-- Create the GUI on script start
createTargetSelectionGUI()
-- Render Step: Continuously update rotation if a target is tracked
RunService.RenderStepped:Connect(function()
if trackedTarget then
smoothRotateToTarget()
end
end)
Certainly! Below is the revised version of your Roblox Lua script with the following enhancements:
Bug Fixes:
smoothRotateToTarget function to properly utilize deltaTime from RenderStepped for smooth interpolation.targetPartsList.Advanced Handlers:
Debugging Enhancements:
DEBUG flag to toggle debug logs easily. Added descriptive debug statements throughout the script to trace execution and state changes.pcall to catch and log potential errors without disrupting the entire script.Code Optimization & Cleanup:
Here's the enhanced script:
--[[
Enhanced Lock-On and ESP Script
- Fixes existing bugs related to rotation and target selection.
- Adds advanced handlers for player removal and target death.
- Implements comprehensive debugging for easier troubleshooting.
--]]
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
-- Configuration
local DEBUG = true -- Toggle debug logs
local ROTATION_SPEED = 5 -- Speed multiplier for rotation interpolation
local ESP_TEXT_PREFIX = "Targeted:" -- Prefix Text displayed in ESP
local ESP_COLOR = Color3.fromRGB(255, 0, 0) -- Color of the ESP text
local GUI_TOGGLE_KEY = Enum.KeyCode.T -- Key to toggle the GUI visibility
-- Debug Function
local function debugLog(message)
if DEBUG then
print("[LockOnScript]: " .. message)
end
end
-- Variables
local player = Players.LocalPlayer
if not player then
error("LocalPlayer not found!")
return
end
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local trackedTarget = nil -- Currently tracked target (another player)
local espIndicator = nil -- BillboardGui for ESP
local selectedPart = "HumanoidRootPart" -- Default targeted part
local guiVisible = true
-- List of possible target parts
local targetPartsListR6 = {
"Left Arm",
"Right Arm",
"Torso",
"Head",
"Left Leg",
"Right Leg"
}
local targetPartsListR15 = {
"LeftUpperArm",
"RightUpperArm",
"UpperTorso",
"Head",
"LeftUpperLeg",
"RightUpperLeg"
}
-- Determine if the character is R6 or R15 using RigType property
local isR15 = humanoid.RigType == Enum.HumanoidRigType.R15
local targetPartsList = isR15 and targetPartsListR15 or targetPartsListR6
debugLog("Rig type determined: " .. (isR15 and "R15" or "R6"))
-- 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
local function findClosestPlayer()
local closestPlayer = nil
local shortestDistance = math.huge
for _, otherPlayer in ipairs(Players:GetPlayers()) do
if otherPlayer ~= player and otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart") then
local distance = (otherPlayer.Character.HumanoidRootPart.Position - humanoidRootPart.Position).Magnitude
if distance < shortestDistance then
shortestDistance = distance
closestPlayer = otherPlayer
end
end
end
if closestPlayer then
debugLog("Closest player found: " .. closestPlayer.Name .. " with distance " .. shortestDistance)
else
debugLog("No closest player found.")
end
return closestPlayer
end
-- Function: Toggle Lock-On to the Closest Player when E is pressed
local function toggleLockOn()
if trackedTarget then
-- Deselect the current target
debugLog("Deselecting current target: " .. (trackedTarget.Name or "Unknown"))
trackedTarget = nil
if espIndicator then
espIndicator:Destroy()
espIndicator = nil
debugLog("ESP indicator destroyed.")
end
else
-- Find and select the closest player
local closestPlayer = findClosestPlayer()
if closestPlayer and closestPlayer.Character then
trackedTarget = closestPlayer.Character
debugLog("Tracking new target: " .. closestPlayer.Name)
-- Create ESP indicator
local targetPart = trackedTarget:FindFirstChild(selectedPart)
if targetPart then
-- Ensure previous ESP is removed
if espIndicator then
espIndicator:Destroy()
espIndicator = nil
debugLog("Previous ESP indicator destroyed.")
end
-- Create BillboardGui for ESP
espIndicator = Instance.new("BillboardGui")
espIndicator.Adornee = targetPart
espIndicator.Size = UDim2.new(0, 100, 0, 50)
espIndicator.StudsOffset = Vector3.new(0, 2, 0)
espIndicator.AlwaysOnTop = true
espIndicator.Name = "TargetESP"
-- Create TextLabel for ESP
local textLabel = Instance.new("TextLabel")
textLabel.Parent = espIndicator
textLabel.Size = UDim2.new(1, 0, 1, 0)
textLabel.BackgroundTransparency = 1
textLabel.Text = ESP_TEXT_PREFIX .. " " .. selectedPart
textLabel.TextColor3 = ESP_COLOR
textLabel.Font = Enum.Font.SourceSansBold
textLabel.TextScaled = true
textLabel.TextStrokeTransparency = 0.8
-- Parent the ESP to the player's PlayerGui
local playerGui = player:WaitForChild("PlayerGui")
espIndicator.Parent = playerGui
debugLog("ESP indicator created for " .. closestPlayer.Name)
else
debugLog("Selected part '" .. selectedPart .. "' not found on " .. closestPlayer.Name)
trackedTarget = nil
end
else
debugLog("No valid closest player to track.")
end
end
end
-- Function: Smoothly rotate to face the tracked target
local function smoothRotateToTarget(deltaTime)
if not trackedTarget then return end
local targetPart = trackedTarget:FindFirstChild(selectedPart)
if targetPart and targetPart:IsA("BasePart") then
local targetPosition = targetPart.Position
-- Calculate direction towards the target on the horizontal plane
local direction = (Vector3.new(targetPosition.X, humanoidRootPart.Position.Y, targetPosition.Z) - humanoidRootPart.Position).Unit
local desiredCFrame = CFrame.new(humanoidRootPart.Position, humanoidRootPart.Position + direction)
-- Smoothly interpolate the CFrame
humanoidRootPart.CFrame = humanoidRootPart.CFrame:Lerp(desiredCFrame, ROTATION_SPEED * deltaTime)
else
debugLog("Target part '" .. selectedPart .. "' is invalid or missing.")
end
end
-- Function: Create Draggable and Hiddable GUI
local function createTargetSelectionGUI()
local success, err = pcall(function()
-- Create ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "TargetSelectionGUI"
screenGui.Parent = player:WaitForChild("PlayerGui")
screenGui.ResetOnSpawn = false
debugLog("TargetSelectionGUI created.")
-- Create Main Frame
local mainFrame = Instance.new("Frame")
mainFrame.Name = "MainFrame"
mainFrame.Size = UDim2.new(0, 200, 0, 150)
mainFrame.Position = UDim2.new(0.5, -100, 0.1, 0)
mainFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
mainFrame.BorderSizePixel = 0
mainFrame.Active = true
-- Since Frame doesn't have a Draggable property by default, implement dragging
local dragging = false
local dragInput, dragStart, startPos
local function update(input)
local delta = input.Position - dragStart
mainFrame.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X,
startPos.Y.Scale, startPos.Y.Offset + delta.Y)
end
mainFrame.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = true
dragStart = input.Position
startPos = mainFrame.Position
input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then
dragging = false
end
end)
end
end)
mainFrame.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
dragInput = input
end
end)
RunService.RenderStepped:Connect(function()
if dragging and dragInput then
update(dragInput)
end
end)
mainFrame.Parent = screenGui
-- Create Title Bar
local titleBar = Instance.new("TextLabel")
titleBar.Name = "TitleBar"
titleBar.Size = UDim2.new(1, 0, 0, 30)
titleBar.Position = UDim2.new(0, 0, 0, 0)
titleBar.BackgroundColor3 = Color3.fromRGB(45, 45, 45)
titleBar.BorderSizePixel = 0
titleBar.Text = "Target Selection"
titleBar.TextColor3 = Color3.fromRGB(255, 255, 255)
titleBar.TextScaled = true
titleBar.Font = Enum.Font.SourceSansBold
titleBar.Parent = mainFrame
debugLog("TitleBar created.")
-- Create Dropdown for Target Parts
local dropdown = Instance.new("Frame")
dropdown.Name = "Dropdown"
dropdown.Size = UDim2.new(0, 180, 0, 30)
dropdown.Position = UDim2.new(0, 10, 0, 40)
dropdown.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
dropdown.Parent = mainFrame
local dropdownLabel = Instance.new("TextLabel")
dropdownLabel.Name = "DropdownLabel"
dropdownLabel.Size = UDim2.new(1, -20, 1, 0)
dropdownLabel.Position = UDim2.new(0, 0, 0, 0)
dropdownLabel.BackgroundTransparency = 1
dropdownLabel.Text = selectedPart
dropdownLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
dropdownLabel.TextScaled = true
dropdownLabel.Font = Enum.Font.SourceSans
dropdownLabel.Parent = dropdown
local dropdownButton = Instance.new("TextButton")
dropdownButton.Name = "DropdownButton"
dropdownButton.Size = UDim2.new(0, 20, 1, 0)
dropdownButton.Position = UDim2.new(1, -20, 0, 0)
dropdownButton.BackgroundTransparency = 1
dropdownButton.Text = "v"
dropdownButton.TextColor3 = Color3.fromRGB(255, 255, 255)
dropdownButton.Font = Enum.Font.SourceSans
dropdownButton.TextScaled = true
dropdownButton.Parent = dropdown
local dropdownList = Instance.new("Frame")
dropdownList.Name = "DropdownList"
dropdownList.Size = UDim2.new(0, 180, 0, 0)
dropdownList.Position = UDim2.new(0, 0, 0, 30)
dropdownList.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
dropdownList.Visible = false
dropdownList.ClipsDescendants = true
dropdownList.Parent = dropdown
local layout = Instance.new("UIListLayout")
layout.HorizontalAlignment = Enum.HorizontalAlignment.Left
layout.Parent = dropdownList
-- Populate Dropdown List
for _, partName in ipairs(targetPartsList) do
local option = Instance.new("TextButton")
option.Name = partName
option.Size = UDim2.new(1, 0, 0, 30)
option.BackgroundTransparency = 1
option.Text = partName
option.TextColor3 = Color3.fromRGB(255, 255, 255)
option.TextScaled = true
option.Font = Enum.Font.SourceSans
option.Parent = dropdownList
option.MouseButton1Click:Connect(function()
selectedPart = partName
dropdownLabel.Text = partName
dropdownList.Visible = false
debugLog("Selected part changed to: " .. partName)
-- Update ESP if tracking a target
if trackedTarget then
if espIndicator then
espIndicator:Destroy()
espIndicator = nil
debugLog("ESP indicator destroyed due to part change.")
end
-- Recreate ESP with the new part
toggleLockOn() -- This will deselect and reselect the target with the new part
end
end)
end
-- Toggle Dropdown List Visibility
dropdownButton.MouseButton1Click:Connect(function()
dropdownList.Visible = not dropdownList.Visible
debugLog("Dropdown list visibility toggled to: " .. tostring(dropdownList.Visible))
end)
-- Close Dropdown if clicking outside using GuiService
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousePos = input.Position
local guiObjects = GuiService:GetGuiObjectsAtPosition(mousePos.X, mousePos.Y)
local clickedInsideDropdown = false
for _, obj in ipairs(guiObjects) do
if dropdown:IsAncestorOf(obj) then
clickedInsideDropdown = true
break
end
end
if not clickedInsideDropdown then
dropdownList.Visible = false
debugLog("Dropdown list closed due to outside click.")
end
end
end
-- Connect the InputBegan event
UserInputService.InputBegan:Connect(onInputBegan)
debugLog("Dropdown created and event connected.")
end)
if not success then
warn("[LockOnScript] Error creating GUI: " .. err)
end
end
-- Function: Toggle GUI Visibility
local function toggleGUIVisibility()
guiVisible = not guiVisible
local gui = player:WaitForChild("PlayerGui"):FindFirstChild("TargetSelectionGUI")
if gui then
gui.Enabled = guiVisible
debugLog("GUI visibility set to: " .. tostring(guiVisible))
else
debugLog("TargetSelectionGUI not found when toggling visibility.")
end
end
-- Clean up ESP when the tracked target dies or leaves
local function onTrackedTargetRemoved()
if not trackedTarget then return end
debugLog("Tracked target removed or died: " .. (trackedTarget.Name or "Unknown"))
trackedTarget = nil
if espIndicator then
espIndicator:Destroy()
espIndicator = nil
debugLog("ESP indicator destroyed due to target removal.")
end
end
-- Event Listeners
-- Toggle Lock-On and GUI Visibility based on key presses
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.E then
toggleLockOn()
elseif input.KeyCode == GUI_TOGGLE_KEY then
toggleGUIVisibility()
end
end)
-- Handle character respawn
player.CharacterAdded:Connect(function(newCharacter)
debugLog("Character respawned.")
character = newCharacter
humanoid = character:WaitForChild("Humanoid")
humanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- Determine rig type using RigType property
isR15 = humanoid.RigType == Enum.HumanoidRigType.R15
targetPartsList = isR15 and targetPartsListR15 or targetPartsListR6
debugLog("New rig type determined: " .. (isR15 and "R15" or "R6"))
-- Reset tracking and ESP on respawn
toggleLockOn()
end)
-- Handle target leaving or dying
local function monitorTrackedTarget(targetCharacter)
if not targetCharacter then return end
targetCharacter:FindFirstChildOfClass("Humanoid").Died:Connect(function()
debugLog("Tracked target " .. (targetCharacter.Name or "Unknown") .. " has died.")
onTrackedTargetRemoved()
end)
targetCharacter.AncestryChanged:Connect(function()
if not targetCharacter:IsDescendantOf(game) then
debugLog("Tracked target " .. (targetCharacter.Name or "Unknown") .. " has left the game.")
onTrackedTargetRemoved()
end
end)
end
-- Modify toggleLockOn to monitor the tracked target
local originalToggleLockOn = toggleLockOn
toggleLockOn = function()
originalToggleLockOn()
if trackedTarget then
monitorTrackedTarget(trackedTarget)
end
end
-- Create the GUI on script start
createTargetSelectionGUI()
-- Render Step: Continuously update rotation if a target is tracked
RunService.RenderStepped:Connect(function(deltaTime)
if trackedTarget then
smoothRotateToTarget(deltaTime)
end
end)
Debugging Enhancements:
DEBUG Flag: A boolean flag DEBUG is introduced at the top of the script. When set to true, the script will print debug logs to the output. This allows you to easily enable or disable debugging without removing the debug statements.debugLog Function: A helper function debugLog is created to handle conditional logging based on the DEBUG flag. This makes the code cleaner and ensures consistent formatting for all debug messages.Smooth Rotation Fix:
smoothRotateToTarget function incorrectly used RunService.RenderStepped:Wait(), which blocks the thread. This has been corrected by passing deltaTime from the RenderStepped event to the function, allowing for smooth interpolation based on frame time.ROTATION_SPEED for a more natural rotation feel. You can tweak this value based on your preference.Advanced Handlers:
monitorTrackedTarget function that listens for the target's death or when they leave the game. If either occurs, the script automatically deselects the target and cleans up the ESP indicator.GUI Improvements:
Draggable property.pcall to catch and log any errors that occur during the GUI setup.Code Optimization:
Integration:
StarterPlayerScripts or StarterCharacterScripts to ensure it runs on the client side.GUI Interaction:
T key (configurable via GUI_TOGGLE_KEY) to toggle the visibility of the Target Selection GUI.E key to toggle lock-on to the closest player. When locked on, your character will smoothly rotate to face the target, and an ESP indicator will appear above the selected body part.Debugging:
DEBUG flag is set to true to view debug logs in the Output window. These logs will help trace the script's execution and identify any issues.DEBUG to false.Customization:
ROTATION_SPEED variable to change how quickly your character rotates to face the target.ESP_TEXT_PREFIX and ESP_COLOR to customize the ESP indicator's text and color.GUI_TOGGLE_KEY to your preferred key for toggling the GUI.This enhanced script provides a more robust and maintainable solution for tracking and ESP functionalities in your Roblox game. The added debugging features will greatly aid in monitoring and troubleshooting, while the advanced handlers ensure that the script behaves correctly across various game states and player interactions.
Feel free to further customize and expand upon this script to suit your game's specific needs!
Not yet included in a published release.
GetServiceFindFirstChildESP Cleanup on Part Change: