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)