Further enhance and add in good back tracking so that it'll always track the back of…
- User
Further enhance and add in good back tracking so that it'll always track the back of someone's torso and a more aesthetic GUI. ALSO layer the lock-on so that it would be more accurate and not ping dependent.
--[[ Enhanced Lock-On and ESP Script with GUI for Body Part Selection and Layered Tracking - Allows selection of specific humanoid body parts: Left Arm, Left Leg, Torso, Torso (Back), Head, Right Leg, Right Arm. - Implements a layered tracking method to prioritize and manage target selections. - Integrates a user-friendly GUI for real-time body part selection. - Maintains all previous functionalities including ShiftLock support, predictive algorithms, and hitbox detection. --]] -- Services local Players = game:GetService("Players") local RunService = game:GetService("RunService") local UserInputService = game:GetService("UserInputService") local GuiService = game:GetService("GuiService") local ContextActionService = game:GetService("ContextActionService") local TweenService = game:GetService("TweenService") local Workspace = game:GetService("Workspace") local CurrentCamera = Workspace.CurrentCamera -- Configuration local DEBUG = true -- Toggle debug logs local ROTATION_SPEED = 25 -- Smooth rotation speed multiplier local ESP_TEXT_PREFIX = "Targeted: " -- Prefix Text displayed in ESP local ESP_COLOR = Color3.fromRGB(255, 0, 0) -- Color of the ESP text local PREDICTION_TIME_BASE = 1.1 -- Base time in seconds to predict target movement local PREDICTION_TIME_ADJUSTMENT_FACTOR = 0.5 -- Factor to adjust prediction time based on velocity and distance local VELOCITY_ALPHA = 0.8 -- Alpha value for Exponential Moving Average local LOCK_ON_KEY = Enum.KeyCode.E -- Key to toggle lock-on local SHIFT_LOCK_TOGGLE_KEY = Enum.KeyCode.LeftShift -- Key to toggle ShiftLock mode local BACK_OFFSET = 5 -- Studs behind the target's torso to aim -- GUI Configuration local GUI_CONFIG = { ScreenGuiName = "LockOnESPGUI", FrameSize = UDim2.new(0, 200, 0, 300), FramePosition = UDim2.new(0, 10, 0.5, -150), ButtonSize = UDim2.new(1, 0, 0, 30), BodyParts = { "Left Arm", "Left Leg", "Torso", "Torso (Back)", "Head", "Right Leg", "Right Arm" }, SelectedColor = Color3.fromRGB(0, 255, 0), UnselectedColor = Color3.fromRGB(255, 255, 255) } -- 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 shiftLockMode = false -- ShiftLock mode toggle local lockOnMode = false -- Lock-On mode toggle local velocityEMA = Vector3.new(0, 0, 0) -- Exponential Moving Average for target velocity local selectedBodyPart = "Torso (Back)" -- Default selected body part -- Determine Rig Type and set the torso part name local isR15 = humanoid.RigType == Enum.HumanoidRigType.R15 local torsoPartName = isR15 and "UpperTorso" or "Torso" debugLog("Rig type determined: " .. (isR15 and "R15" or "R6")) debugLog("Torso part set to: " .. torsoPartName) -- Mapping for R6 and R15 body parts local bodyPartMappings = { ["Left Arm"] = isR15 and "LeftUpperArm" or "Left Arm", ["Left Leg"] = isR15 and "LeftUpperLeg" or "Left Leg", ["Torso"] = torsoPartName, ["Torso (Back)"] = torsoPartName, -- Special handling ["Head"] = "Head", ["Right Leg"] = isR15 and "RightUpperLeg" or "Right Leg", ["Right Arm"] = isR15 and "RightUpperArm" or "Right Arm" } -- 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 hasHumanoid(otherPlayer.Character) then local targetPartName = bodyPartMappings[selectedBodyPart] local otherTorso = otherPlayer.Character:FindFirstChild(targetPartName) if otherTorso and otherTorso:IsA("BasePart") then local distance = (otherTorso.Position - humanoidRootPart.Position).Magnitude if distance < shortestDistance then shortestDistance = distance closestPlayer = otherPlayer end end end end if closestPlayer then debugLog("Closest player found: " .. closestPlayer.Name .. " with distance " .. string.format("%.2f", shortestDistance)) else debugLog("No closest player found.") end return closestPlayer end -- Function: Toggle Lock-On to the Closest Player when LOCK_ON_KEY is pressed local function toggleLockOn() if lockOnMode then -- Deselect the current target debugLog("Deselecting current target: " .. (trackedTarget and trackedTarget.Name or "None")) trackedTarget = nil lockOnMode = false 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 lockOnMode = true debugLog("Tracking new target: " .. closestPlayer.Name) -- Create ESP indicator local targetPartName = bodyPartMappings[selectedBodyPart] local targetPart = trackedTarget:FindFirstChild(targetPartName) 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, 150, 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 .. trackedTarget.Name .. " (" .. selectedBodyPart .. ")" 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 .. " targeting " .. selectedBodyPart) else debugLog("Selected body part '" .. targetPartName .. "' not found on " .. closestPlayer.Name) trackedTarget = nil end else debugLog("No valid closest player to track.") end end end -- Function: Predict Future Position Based on Target's Velocity using EMA local function getPredictedPosition(targetCharacter) local targetPartName = bodyPartMappings[selectedBodyPart] local targetPart = targetCharacter:FindFirstChild(targetPartName) if not targetPart then debugLog("Target does not have the part: " .. targetPartName) return nil end -- Get current velocity local currentVelocity = targetPart.Velocity -- Update Exponential Moving Average (EMA) for velocity velocityEMA = (currentVelocity * VELOCITY_ALPHA) + (velocityEMA * (1 - VELOCITY_ALPHA)) -- Calculate dynamic prediction time based on velocity magnitude and distance local distance = (targetPart.Position - humanoidRootPart.Position).Magnitude local velocityMagnitude = velocityEMA.Magnitude local predictionTime = PREDICTION_TIME_BASE + (velocityMagnitude * PREDICTION_TIME_ADJUSTMENT_FACTOR) + (distance * PREDICTION_TIME_ADJUSTMENT_FACTOR) predictionTime = math.clamp(predictionTime, 0.2, 0.8) -- Clamp prediction time between 0.2 and 0.8 seconds -- Calculate predicted position local futurePosition = targetPart.Position + (velocityEMA * predictionTime) debugLog(string.format("Predicted position: (%.2f, %.2f, %.2f); Prediction Time: %.2f", futurePosition.X, futurePosition.Y, futurePosition.Z, predictionTime)) -- Special Handling for "Torso (Back)" if selectedBodyPart == "Torso (Back)" then if velocityEMA.Magnitude > 0 then -- If the target is moving, assume back is opposite to their velocity local backDirection = -velocityEMA.Unit futurePosition = futurePosition + (backDirection * BACK_OFFSET) debugLog("Calculated back position based on movement.") else -- If the target is stationary, use their current orientation futurePosition = futurePosition - (targetPart.CFrame.LookVector * BACK_OFFSET) debugLog("Calculated back position based on orientation.") end end debugLog(string.format("Final target position: (%.2f, %.2f, %.2f)", futurePosition.X, futurePosition.Y, futurePosition.Z)) return futurePosition end -- Function: Instantly rotate to face the predicted target position local function instantRotateToPosition(targetPosition) if not targetPosition then return end local desiredCFrame = CFrame.new(humanoidRootPart.Position, targetPosition) humanoidRootPart.CFrame = desiredCFrame debugLog("Instantly rotated to face target position.") end -- Function: Smoothly rotate to face the predicted target position local function smoothRotateToPosition(targetPosition, deltaTime) if not targetPosition then return end 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) debugLog("Smoothly rotated to face target position.") end -- Function: Rotate to Target (Instant or Smooth based on toggle) local function rotateToTarget(deltaTime) if not trackedTarget then return end local targetPartName = bodyPartMappings[selectedBodyPart] local targetPart = trackedTarget:FindFirstChild(targetPartName) if targetPart and targetPart:IsA("BasePart") then local predictedPosition = getPredictedPosition(trackedTarget) if predictedPosition then if shiftLockMode then instantRotateToPosition(predictedPosition) else smoothRotateToPosition(predictedPosition, deltaTime) end -- Hitbox Detection: Ensure predicted position is within part bounds local partSize = targetPart.Size local partPosition = targetPart.Position local deltaPosition = predictedPosition - partPosition local withinX = math.abs(deltaPosition.X) <= (partSize.X / 2 + (selectedBodyPart == "Torso (Back)" and BACK_OFFSET or 0)) local withinY = math.abs(deltaPosition.Y) <= (partSize.Y / 2) local withinZ = math.abs(deltaPosition.Z) <= (partSize.Z / 2 + (selectedBodyPart == "Torso (Back)" and BACK_OFFSET or 0)) if withinX and withinY and withinZ then debugLog("Predicted position is within the part's hitbox.") else debugLog("Predicted position is outside the part's hitbox.") end else debugLog("Failed to get predicted target position.") end else debugLog("Target part '" .. targetPartName .. "' is invalid or missing.") end end -- Function: Handle ShiftLock Toggle local function toggleShiftLock() if shiftLockMode then -- Disable ShiftLock Mode shiftLockMode = false CurrentCamera.CameraType = Enum.CameraType.Custom debugLog("ShiftLock mode disabled.") else if lockOnMode then -- Disable Lock-On before enabling ShiftLock to prevent conflicts toggleLockOn() end -- Enable ShiftLock Mode shiftLockMode = true CurrentCamera.CameraType = Enum.CameraType.Scriptable -- Lock the camera to look at the target if trackedTarget then local targetPartName = bodyPartMappings[selectedBodyPart] local targetPart = trackedTarget:FindFirstChild(targetPartName) if targetPart then local predictedPosition = getPredictedPosition(trackedTarget) CurrentCamera.CFrame = CFrame.new(humanoidRootPart.Position, predictedPosition) debugLog("ShiftLock mode enabled and camera locked onto target.") end end end end -- Function: 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 lockOnMode = false if espIndicator then espIndicator:Destroy() espIndicator = nil debugLog("ESP indicator destroyed due to target removal.") end velocityEMA = Vector3.new(0, 0, 0) -- Reset velocity EMA -- Reset camera if ShiftLock was active if shiftLockMode then toggleShiftLock() end end -- Function: Monitor Tracked Target for Removal or Death local function monitorTrackedTarget(targetCharacter) if not targetCharacter then return end local humanoid = targetCharacter:FindFirstChildOfClass("Humanoid") if humanoid then humanoid.Died:Connect(function() debugLog("Tracked target " .. (targetCharacter.Name or "Unknown") .. " has died.") onTrackedTargetRemoved() end) 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 -- Function: 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 torsoPartName = isR15 and "UpperTorso" or "Torso" debugLog("New rig type determined: " .. (isR15 and "R15" or "R6")) -- Reset tracking and ESP on respawn onTrackedTargetRemoved() end) -- Function: Update Camera during ShiftLock Mode local function updateCamera(deltaTime) if shiftLockMode and trackedTarget then local targetPartName = bodyPartMappings[selectedBodyPart] local targetPart = trackedTarget:FindFirstChild(targetPartName) if targetPart then -- Smoothly move the camera to follow the target local predictedPosition = getPredictedPosition(trackedTarget) if predictedPosition then local desiredCFrame = CFrame.new(humanoidRootPart.Position, predictedPosition) CurrentCamera.CFrame = CurrentCamera.CFrame:lerp(desiredCFrame, ROTATION_SPEED * deltaTime) debugLog("Camera updated to follow target in ShiftLock mode.") end end end end -- Function: Create and Setup the Body Part Selection GUI local function createBodyPartSelectionGUI() -- Check if the GUI already exists to prevent duplication if player.PlayerGui:FindFirstChild(GUI_CONFIG.ScreenGuiName) then debugLog("GUI already exists. Skipping creation.") return end -- Create ScreenGui local screenGui = Instance.new("ScreenGui") screenGui.Name = GUI_CONFIG.ScreenGuiName screenGui.Parent = player.PlayerGui screenGui.ResetOnSpawn = false -- Create Frame local frame = Instance.new("Frame") frame.Size = GUI_CONFIG.FrameSize frame.Position = GUI_CONFIG.FramePosition frame.BackgroundTransparency = 0.5 frame.BackgroundColor3 = Color3.fromRGB(0, 0, 0) frame.Parent = screenGui -- Create UIListLayout for organized buttons local uiListLayout = Instance.new("UIListLayout") uiListLayout.Parent = frame uiListLayout.FillDirection = Enum.FillDirection.Vertical uiListLayout.SortOrder = Enum.SortOrder.LayoutOrder uiListLayout.Padding = UDim.new(0, 5) -- Function to handle body part selection local function selectBodyPart(bodyPart, button) selectedBodyPart = bodyPart debugLog("Selected body part: " .. bodyPart) toggleLockOn() -- Deselect current target -- Update ESP text if a target is already locked on if lockOnMode and espIndicator then espIndicator:Destroy() espIndicator = nil debugLog("ESP indicator destroyed due to body part change.") toggleLockOn() -- Re-select target with new body part end -- Update button colors to indicate selection for _, btn in ipairs(frame:GetChildren()) do if btn:IsA("TextButton") then btn.BackgroundColor3 = GUI_CONFIG.UnselectedColor end end button.BackgroundColor3 = GUI_CONFIG.SelectedColor end -- Create Buttons for each Body Part for _, bodyPart in ipairs(GUI_CONFIG.BodyParts) do local button = Instance.new("TextButton") button.Size = GUI_CONFIG.ButtonSize button.Text = bodyPart button.Font = Enum.Font.SourceSans button.TextSize = 18 button.TextColor3 = Color3.fromRGB(255, 255, 255) button.BackgroundColor3 = (bodyPart == selectedBodyPart) and GUI_CONFIG.SelectedColor or GUI_CONFIG.UnselectedColor button.AutoButtonColor = false button.Parent = frame button.MouseButton1Click:Connect(function() selectBodyPart(bodyPart, button) end) end debugLog("Body Part Selection GUI created.") end -- Call the function to create the GUI createBodyPartSelectionGUI() -- Function: Setup ShiftLock and Lock-On Toggles local function setupShiftLockToggle() UserInputService.InputBegan:Connect(function(input, gameProcessed) if gameProcessed then return end if input.KeyCode == LOCK_ON_KEY then toggleLockOn() elseif input.KeyCode == SHIFT_LOCK_TOGGLE_KEY then toggleShiftLock() end end) end -- Event Listeners -- Setup ShiftLock and Lock-On Toggles setupShiftLockToggle() -- Render Step: Continuously update rotation and camera if a target is tracked RunService.RenderStepped:Connect(function(deltaTime) if lockOnMode and trackedTarget then pcall(function() rotateToTarget(deltaTime) updateCamera(deltaTime) end) end -- Close Proximity Check Removed -- The handleCloseProximity function and related checks have been removed as per the latest request. end) - Assistant