-- KS-Rivals Script Hub (Full Auto Integration & HUB2 Update + Rage Bot - Fixed + Optimized + Loading System v2.2 + Team Check)
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local VirtualInputManager = game:GetService("VirtualInputManager")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Camera = workspace.CurrentCamera

-- ============================================
-- TEAM CHECK SYSTEM
-- ============================================

local function getTeamID(player)
    local teamID = player:GetAttribute("TeamID")
    if teamID then return teamID end
    
    if player.Character then
        local teamValue = player.Character:FindFirstChild("TeamID")
        if teamValue then
            if teamValue:IsA("StringValue") or teamValue:IsA("IntValue") then
                return teamValue.Value
            end
        end
    end
    
    if player.Team then
        return player.Team.Name
    end
    
    return nil
end

local function isEnemy(player)
    if not player or player == LocalPlayer then
        return false
    end
    
    local myTeam = getTeamID(LocalPlayer)
    local theirTeam = getTeamID(player)
    
    if not myTeam or not theirTeam then
        return false
    end
    
    return myTeam ~= theirTeam
end

-- ============================================
-- CONFIG SYSTEM (Global Variables)
-- ============================================

local ESP_ENABLED = false
local SILENT_AIM_ENABLED = false
local HP_BAR_ENABLED = false
local AIM_LOCK_ENABLED = false
local AUTO_AIM_ENABLED = false
local NOTIFICATION_ENABLED = false
local AUTO_MATCH_ENABLED = false
local FULL_AUTO_ENABLED = false
local SKIN_CHANGER_ENABLED = false
local NO_COOL_TIME_ENABLED = false
local RAGE_BOT_ENABLED = false
local THIRD_PERSON_ENABLED = false
local NO_RECOIL_ENABLED = false
local AUTO_AIM_MODE = "Silent"
local AUTO_MATCH_MODE = "1v1"
local AIM_LOCK_KEY = Enum.UserInputType.MouseButton2
local RAGE_BOT_KEY = Enum.KeyCode.Five
local isAimLockHeld = false
local aimLockConnection
local lastClickTime = 0
local CLICK_INTERVAL = 0.01

-- Loading System Variables
local LOADING_COMPLETE = false
local HUB_ANIMATION_PLAYING = false
local LOADING_ERRORS = {}

-- Toggle Check Inner References
local ToggleCheckInners = {}

-- Third Person Variables
local thirdPersonConnection = nil
local thirdPersonWheelConnection = nil
local distance = 12
local heightOffset = 2.5
local sideOffset = 0
local MIN_DISTANCE = 3
local MAX_DISTANCE = 30
local ZOOM_SPEED = 1.5

-- Full Auto �꾩슜 蹂���
local faTargetPlayer = nil
local faIsAbove = false
local faLastHealth = 100
local faTargetLastHealth = 100
local faTargetMaxHealth = 100
local faSpinConnection = nil
local faAutoClickConnection = nil
local faCameraConnection = nil
local faIsFirstTeleport = true
local faLoopActive = false
local faLastAutoClickTime = 0
local FA_CLICK_INTERVAL = 0.02
local faAutoClickPaused = false
local faGravityConnection = nil
local faLastRetreatTime = 0
local FA_RETREAT_INTERVAL = 2.5
local faIsRetreating = false
local faRetreatLock = false

-- No Cool Time �꾩슜 蹂���
local noCoolTimeConnection = nil
local noCoolTimeApplied = false
local weaponDataCache = nil
local lastCooldownRemoval = 0
local COOLDOWN_REMOVAL_INTERVAL = 10

-- Rage Bot �꾩슜 蹂���
local rbEnabled = false
local rbTarget = nil
local rbConnection = nil
local rbChar = nil
local rbHrp = nil
local rbHum = nil

-- No Recoil �꾩슜 蹂���
local nrGunModule = nil
local nrItemLibrary = nil
local nrOriginalRecoil = nil
local nrOriginalStartShooting = nil
local nrOriginal_LocalTracers = nil
local nrCrosshairProtected = false

-- Silent Aim �꾩슜 蹂���
local silentAimActive = false
local oldNamecall = nil
local metaTableHooked = false

local function saveConfig()
    local config = {
        ESP_ENABLED = ESP_ENABLED,
        SILENT_AIM_ENABLED = SILENT_AIM_ENABLED,
        HP_BAR_ENABLED = HP_BAR_ENABLED,
        AIM_LOCK_ENABLED = AIM_LOCK_ENABLED,
        AUTO_AIM_ENABLED = AUTO_AIM_ENABLED,
        NOTIFICATION_ENABLED = NOTIFICATION_ENABLED,
        AUTO_MATCH_ENABLED = AUTO_MATCH_ENABLED,
        FULL_AUTO_ENABLED = FULL_AUTO_ENABLED,
        SKIN_CHANGER_ENABLED = SKIN_CHANGER_ENABLED,
        NO_COOL_TIME_ENABLED = NO_COOL_TIME_ENABLED,
        RAGE_BOT_ENABLED = RAGE_BOT_ENABLED,
        THIRD_PERSON_ENABLED = THIRD_PERSON_ENABLED,
        NO_RECOIL_ENABLED = NO_RECOIL_ENABLED,
        AUTO_AIM_MODE = AUTO_AIM_MODE,
        AUTO_MATCH_MODE = AUTO_MATCH_MODE,
        AIM_LOCK_KEY = tostring(AIM_LOCK_KEY),
        RAGE_BOT_KEY = tostring(RAGE_BOT_KEY)
    }
    pcall(function()
        writefile("KSRivals_Config.json", HttpService:JSONEncode(config))
    end)
end

local function loadConfig()
    local success, result = pcall(function()
        if isfile and isfile("KSRivals_Config.json") then
            return HttpService:JSONDecode(readfile("KSRivals_Config.json"))
        end
        return nil
    end)
    if success then return result end
    return nil
end

local savedConfig = loadConfig()
if savedConfig then
    ESP_ENABLED = savedConfig.ESP_ENABLED or false
    SILENT_AIM_ENABLED = savedConfig.SILENT_AIM_ENABLED or false
    HP_BAR_ENABLED = savedConfig.HP_BAR_ENABLED or false
    AIM_LOCK_ENABLED = savedConfig.AIM_LOCK_ENABLED or false
    AUTO_AIM_ENABLED = savedConfig.AUTO_AIM_ENABLED or false
    NOTIFICATION_ENABLED = savedConfig.NOTIFICATION_ENABLED or false
    AUTO_MATCH_ENABLED = savedConfig.AUTO_MATCH_ENABLED or false
    FULL_AUTO_ENABLED = savedConfig.FULL_AUTO_ENABLED or false
    SKIN_CHANGER_ENABLED = savedConfig.SKIN_CHANGER_ENABLED or false
    NO_COOL_TIME_ENABLED = savedConfig.NO_COOL_TIME_ENABLED or false
    RAGE_BOT_ENABLED = savedConfig.RAGE_BOT_ENABLED or false
    THIRD_PERSON_ENABLED = savedConfig.THIRD_PERSON_ENABLED or false
    NO_RECOIL_ENABLED = savedConfig.NO_RECOIL_ENABLED or false
    AUTO_AIM_MODE = savedConfig.AUTO_AIM_MODE or "Silent"
    AUTO_MATCH_MODE = savedConfig.AUTO_MATCH_MODE or "1v1"
    if savedConfig.AIM_LOCK_KEY then
        pcall(function()
            if savedConfig.AIM_LOCK_KEY:find("MouseButton") then
                AIM_LOCK_KEY = Enum.UserInputType[savedConfig.AIM_LOCK_KEY:split(".")[3]]
            else
                AIM_LOCK_KEY = Enum.KeyCode[savedConfig.AIM_LOCK_KEY:split(".")[3]]
            end
        end)
    end
    if savedConfig.RAGE_BOT_KEY then
        pcall(function()
            RAGE_BOT_KEY = Enum.KeyCode[savedConfig.RAGE_BOT_KEY:split(".")[3]]
        end)
    end
end

-- ============================================
-- AUTO-RELOAD SYSTEM
-- ============================================

local SCRIPT_URL = "https://[Log in to view URL]"
local BOOTSTRAP_CODE = [[
repeat task.wait() until game:IsLoaded()
task.wait(2)
pcall(function()
    loadstring(game:HttpGet("]] .. SCRIPT_URL .. [[", true))()
end)
]]

local function setupAutoReload()
    local qot = syn and syn.queue_on_teleport or queue_on_teleport or queueonteleport
    if qot then pcall(function() qot(BOOTSTRAP_CODE) end) end
end

setupAutoReload()
LocalPlayer.OnTeleport:Connect(function(state)
    if state == Enum.TeleportState.Started then
        saveConfig()
        setupAutoReload()
    end
end)

-- ============================================
-- NOTIFICATION SYSTEM
-- ============================================

pcall(function()
    for _, gui in ipairs(LocalPlayer.PlayerGui:GetChildren()) do
        if gui.Name == "NotificationSystem" then gui:Destroy() end
    end
end)

local NotificationGui = Instance.new("ScreenGui")
NotificationGui.Name = "NotificationSystem"
NotificationGui.Parent = LocalPlayer.PlayerGui
NotificationGui.ResetOnSpawn = false

local NotifContainer = Instance.new("Frame")
NotifContainer.Size = UDim2.new(0, 200, 0, 400)
NotifContainer.Position = UDim2.new(0, 10, 0, 10)
NotifContainer.BackgroundTransparency = 1
NotifContainer.Parent = NotificationGui

local NotifLayout = Instance.new("UIListLayout")
NotifLayout.SortOrder = Enum.SortOrder.LayoutOrder
NotifLayout.Padding = UDim.new(0, 4)
NotifLayout.Parent = NotifContainer

local function createNotification(text)
    if not NOTIFICATION_ENABLED then return end
    local notif = Instance.new("Frame")
    notif.Size = UDim2.new(0, 0, 0, 22)
    notif.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
    notif.BackgroundTransparency = 0.2
    notif.BorderSizePixel = 0
    notif.ClipsDescendants = true
    notif.Parent = NotifContainer
    
    local corner = Instance.new("UICorner")
    corner.CornerRadius = UDim.new(0, 4)
    corner.Parent = notif
    
    local label = Instance.new("TextLabel")
    label.Size = UDim2.new(1, -10, 1, 0)
    label.Position = UDim2.new(0, 5, 0, 0)
    label.BackgroundTransparency = 1
    label.Text = text
    label.TextColor3 = Color3.fromRGB(255, 255, 255)
    label.TextSize = 12
    label.Font = Enum.Font.GothamBold
    label.TextXAlignment = Enum.TextXAlignment.Left
    label.Parent = notif
    
    TweenService:Create(notif, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Size = UDim2.new(1, 0, 0, 22)}):Play()
    
    task.delay(2, function()
        if notif and notif.Parent then
            local closeTween = TweenService:Create(notif, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Size = UDim2.new(0, 0, 0, 22)})
            closeTween:Play()
            closeTween.Completed:Connect(function() 
                if notif then notif:Destroy() end
            end)
        end
    end)
end

-- ============================================
-- SILENT AIM SYSTEM (NEW RAYCAST HOOK)
-- ============================================

local function isVisible(targetHead)
    if not targetHead then return false end
    local character = LocalPlayer.Character
    if not character then return false end
    
    local rayParams = RaycastParams.new()
    rayParams.FilterDescendantsInstances = {character}
    rayParams.FilterType = Enum.RaycastFilterType.Exclude
    
    local origin = Camera.CFrame.Position
    local direction = (targetHead.Position - origin)
    local ray = workspace:Raycast(origin, direction, rayParams)
    
    if not ray then return true end
    return ray.Instance:IsDescendantOf(targetHead.Parent)
end

local function getClosestPlayerForSilentAim()
    if not SILENT_AIM_ENABLED then return nil end
    
    local character = LocalPlayer.Character
    if not character or not character:FindFirstChild("HumanoidRootPart") then
        return nil
    end
    
    local closestPlayer = nil
    local shortestScreenDistance = math.huge
    local screenCenter = Vector2.new(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)
    
    for _, player in ipairs(Players:GetPlayers()) do
        if player ~= LocalPlayer and isEnemy(player) and player.Character then
            local head = player.Character:FindFirstChild("Head")
            local humanoid = player.Character:FindFirstChild("Humanoid")
            
            if head and humanoid and humanoid.Health > 0 then
                local screenPos, onScreen = Camera:WorldToScreenPoint(head.Position)
                
                if onScreen and isVisible(head) then
                    local screenDistance = (Vector2.new(screenPos.X, screenPos.Y) - screenCenter).Magnitude
                    
                    if screenDistance < shortestScreenDistance then
                        shortestScreenDistance = screenDistance
                        closestPlayer = player
                    end
                end
            end
        end
    end
    
    return closestPlayer
end

local function createFakeRaycastResult(targetHead, rayOrigin)
    if not targetHead then return nil end
    
    local direction = (targetHead.Position - rayOrigin).Unit
    local distance = (targetHead.Position - rayOrigin).Magnitude
    
    local fakeResult = {
        Instance = targetHead,
        Position = targetHead.Position,
        Distance = distance,
        Material = targetHead.Material,
        Normal = -direction,
    }
    
    return fakeResult
end

local function shouldHookRaycast(origin, direction, raycastParams)
    if not SILENT_AIM_ENABLED then return false end
    if not raycastParams then return false end
    
    if raycastParams.FilterType ~= Enum.RaycastFilterType.Include then
        return false
    end
    
    local rayDistance = direction.Magnitude
    if rayDistance < 10 then return false end
    
    local camPos = Camera.CFrame.Position
    local distanceFromCamera = (origin - camPos).Magnitude
    
    if distanceFromCamera > 5 then return false end
    
    return true
end

local function setupSilentAimHook()
    if metaTableHooked then return end
    
    local success, mt = pcall(function()
        return getrawmetatable(game)
    end)
    
    if not success then
        mt = getmetatable(game)
    end
    
    oldNamecall = mt.__namecall
    
    pcall(function()
        setreadonly(mt, false)
    end)
    
    pcall(function()
        make_writeable(mt)
    end)
    
    mt.__namecall = newcclosure and newcclosure(function(self, ...)
        local method = getnamecallmethod()
        local args = {...}
        
        if method == "Raycast" and self == workspace then
            local origin = args[1]
            local direction = args[2]
            local raycastParams = args[3]
            
            if shouldHookRaycast(origin, direction, raycastParams) then
                local closestPlayer = getClosestPlayerForSilentAim()
                if closestPlayer and closestPlayer.Character then
                    local targetHead = closestPlayer.Character:FindFirstChild("Head")
                    if targetHead then
                        return createFakeRaycastResult(targetHead, origin)
                    end
                end
                return nil
            end
        end
        
        return oldNamecall(self, ...)
    end) or function(self, ...)
        local method = getnamecallmethod()
        local args = {...}
        
        if method == "Raycast" and self == workspace then
            local origin = args[1]
            local direction = args[2]
            local raycastParams = args[3]
            
            if shouldHookRaycast(origin, direction, raycastParams) then
                local closestPlayer = getClosestPlayerForSilentAim()
                if closestPlayer and closestPlayer.Character then
                    local targetHead = closestPlayer.Character:FindFirstChild("Head")
                    if targetHead then
                        return createFakeRaycastResult(targetHead, origin)
                    end
                end
                return nil
            end
        end
        
        return oldNamecall(self, ...)
    end
    
    pcall(function()
        setreadonly(mt, true)
    end)
    
    pcall(function()
        make_readonly(mt)
    end)
    
    metaTableHooked = true
end

setupSilentAimHook()

-- ============================================
-- UI SETUP
-- ============================================

pcall(function()
    for _, gui in ipairs(game:GetService("CoreGui"):GetChildren()) do
        if gui.Name == "KSRivals_Hub" then gui:Destroy() end
    end
end)

local KSRivals = Instance.new("ScreenGui")
KSRivals.Name = "KSRivals_Hub"
KSRivals.ResetOnSpawn = false

pcall(function()
    KSRivals.Parent = game:GetService("CoreGui")
end)
if not KSRivals.Parent then
    KSRivals.Parent = LocalPlayer.PlayerGui
end

local MainFrame = Instance.new("Frame")
MainFrame.Name = "MainFrame"
MainFrame.Parent = KSRivals
MainFrame.BackgroundColor3 = Color3.fromRGB(8, 8, 8)
MainFrame.Position = UDim2.new(0.5, -240, 0.5, -160)
MainFrame.Size = UDim2.new(0, 480, 0, 320)
MainFrame.BorderSizePixel = 0
MainFrame.Visible = false
MainFrame.ClipsDescendants = true

local MainStroke = Instance.new("UIStroke")
MainStroke.Color = Color3.fromRGB(255, 255, 255)
MainStroke.Thickness = 1
MainStroke.Parent = MainFrame

-- ============================================
-- KEYBIND INFO UI (Enhanced)
-- ============================================

local KeybindInfoFrame = Instance.new("Frame")
KeybindInfoFrame.Name = "KeybindInfoFrame"
KeybindInfoFrame.Parent = KSRivals
KeybindInfoFrame.BackgroundColor3 = Color3.fromRGB(8, 8, 8)
KeybindInfoFrame.BackgroundTransparency = 0.3
KeybindInfoFrame.Position = UDim2.new(1, -180, 0.5, -50)
KeybindInfoFrame.Size = UDim2.new(0, 170, 0, 100)
KeybindInfoFrame.BorderSizePixel = 0
KeybindInfoFrame.Visible = false
KeybindInfoFrame.ClipsDescendants = true

local KeybindStroke = Instance.new("UIStroke")
KeybindStroke.Color = Color3.fromRGB(255, 255, 255)
KeybindStroke.Thickness = 1
KeybindStroke.Transparency = 0.3
KeybindStroke.Parent = KeybindInfoFrame

local KeybindCorner = Instance.new("UICorner")
KeybindCorner.CornerRadius = UDim.new(0, 4)
KeybindCorner.Parent = KeybindInfoFrame

local KeybindTitle = Instance.new("TextLabel")
KeybindTitle.Name = "KeybindTitle"
KeybindTitle.Parent = KeybindInfoFrame
KeybindTitle.BackgroundTransparency = 1
KeybindTitle.Position = UDim2.new(0, 10, 0, 8)
KeybindTitle.Size = UDim2.new(1, -20, 0, 16)
KeybindTitle.Font = Enum.Font.GothamBold
KeybindTitle.Text = "KEYBINDS"
KeybindTitle.TextColor3 = Color3.fromRGB(255, 255, 255)
KeybindTitle.TextSize = 12
KeybindTitle.TextXAlignment = Enum.TextXAlignment.Left

local KeybindInfo = Instance.new("TextLabel")
KeybindInfo.Name = "KeybindInfo"
KeybindInfo.Parent = KeybindInfoFrame
KeybindInfo.BackgroundTransparency = 1
KeybindInfo.Position = UDim2.new(0, 10, 0, 28)
KeybindInfo.Size = UDim2.new(1, -20, 1, -36)
KeybindInfo.Font = Enum.Font.Code
KeybindInfo.Text = ""
KeybindInfo.TextColor3 = Color3.fromRGB(200, 200, 200)
KeybindInfo.TextSize = 11
KeybindInfo.TextXAlignment = Enum.TextXAlignment.Left
KeybindInfo.TextYAlignment = Enum.TextYAlignment.Top
KeybindInfo.TextWrapped = true

-- ============================================
-- KEYBIND INFO UPDATE FUNCTION
-- ============================================

local function formatKeyName(enumItem)
    local name = enumItem.Name
    if name == "MouseButton1" then return "MB1" end
    if name == "MouseButton2" then return "MB2" end
    return name
end

local function UpdateKeybindInfo()
    local keybindLines = {}
    local lineCount = 0
    
    table.insert(keybindLines, "Hub Toggle: [K]")
    lineCount = lineCount + 1
    
    if AIM_LOCK_ENABLED then
        table.insert(keybindLines, "Aim Lock: [" .. formatKeyName(AIM_LOCK_KEY) .. "]")
        lineCount = lineCount + 1
    end
    
    if RAGE_BOT_ENABLED then
        table.insert(keybindLines, "Rage Bot: [" .. formatKeyName(RAGE_BOT_KEY) .. "]")
        lineCount = lineCount + 1
    end
    
    KeybindInfo.Text = table.concat(keybindLines, "\n")
    
    local newHeight = (lineCount * 15) + 30
    KeybindInfoFrame.Size = UDim2.new(0, 170, 0, newHeight)
    KeybindInfoFrame.Position = UDim2.new(1, -180, 0.5, -newHeight/2)
end

-- ============================================
-- KEYBIND INFO ANIMATION FUNCTIONS
-- ============================================

local KEYBIND_ANIMATION_PLAYING = false

local function ShowKeybindInfo()
    if KEYBIND_ANIMATION_PLAYING or MainFrame.Visible then return end
    KEYBIND_ANIMATION_PLAYING = true
    
    UpdateKeybindInfo()
    
    local currentHeight = KeybindInfoFrame.Size.Y.Offset
    
    KeybindInfoFrame.Size = UDim2.new(0, 0, 0, currentHeight)
    KeybindInfoFrame.Position = UDim2.new(1, 0, 0.5, -currentHeight/2)
    KeybindInfoFrame.BackgroundTransparency = 1
    KeybindStroke.Transparency = 1
    KeybindTitle.TextTransparency = 1
    KeybindInfo.TextTransparency = 1
    KeybindInfoFrame.Visible = true
    
    local slideTween = TweenService:Create(KeybindInfoFrame, TweenInfo.new(0.3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {
        Size = UDim2.new(0, 170, 0, currentHeight),
        Position = UDim2.new(1, -180, 0.5, -currentHeight/2),
        BackgroundTransparency = 0.3
    })
    
    local strokeTween = TweenService:Create(KeybindStroke, TweenInfo.new(0.3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {
        Transparency = 0.3
    })
    
    slideTween:Play()
    strokeTween:Play()
    
    slideTween.Completed:Connect(function()
        TweenService:Create(KeybindTitle, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
            TextTransparency = 0
        }):Play()
        
        TweenService:Create(KeybindInfo, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
            TextTransparency = 0.2
        }):Play()
        
        task.wait(0.2)
        KEYBIND_ANIMATION_PLAYING = false
    end)
end

local function HideKeybindInfo()
    if KEYBIND_ANIMATION_PLAYING then return end
    KEYBIND_ANIMATION_PLAYING = true
    
    TweenService:Create(KeybindTitle, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
        TextTransparency = 1
    }):Play()
    
    TweenService:Create(KeybindInfo, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
        TextTransparency = 1
    }):Play()
    
    task.wait(0.15)
    
    local currentHeight = KeybindInfoFrame.Size.Y.Offset
    
    local slideTween = TweenService:Create(KeybindInfoFrame, TweenInfo.new(0.25, Enum.EasingStyle.Quart, Enum.EasingDirection.In), {
        Size = UDim2.new(0, 0, 0, currentHeight),
        Position = UDim2.new(1, 0, 0.5, -currentHeight/2),
        BackgroundTransparency = 1
    })
    
    local strokeTween = TweenService:Create(KeybindStroke, TweenInfo.new(0.25, Enum.EasingStyle.Quart, Enum.EasingDirection.In), {
        Transparency = 1
    })
    
    slideTween:Play()
    strokeTween:Play()
    
    slideTween.Completed:Connect(function()
        KeybindInfoFrame.Visible = false
        KeybindInfoFrame.BackgroundTransparency = 0.3
        KeybindStroke.Transparency = 0.3
        KEYBIND_ANIMATION_PLAYING = false
    end)
end

-- ============================================
-- LOADING UI SYSTEM
-- ============================================

local LoadingOverlay = Instance.new("Frame")
LoadingOverlay.Name = "LoadingOverlay"
LoadingOverlay.Parent = MainFrame
LoadingOverlay.BackgroundColor3 = Color3.fromRGB(8, 8, 8)
LoadingOverlay.Size = UDim2.new(1, 0, 1, 0)
LoadingOverlay.Position = UDim2.new(0, 0, 0, 0)
LoadingOverlay.ZIndex = 100
LoadingOverlay.BorderSizePixel = 0
LoadingOverlay.Visible = true

local LoadingTitle = Instance.new("TextLabel")
LoadingTitle.Name = "LoadingTitle"
LoadingTitle.Parent = LoadingOverlay
LoadingTitle.BackgroundTransparency = 1
LoadingTitle.Position = UDim2.new(0.5, 0, 0.28, 0)
LoadingTitle.Size = UDim2.new(0.8, 0, 0, 35)
LoadingTitle.AnchorPoint = Vector2.new(0.5, 0.5)
LoadingTitle.Font = Enum.Font.GothamBlack
LoadingTitle.Text = "KS-RIVALS HUB"
LoadingTitle.TextColor3 = Color3.fromRGB(255, 255, 255)
LoadingTitle.TextSize = 24
LoadingTitle.ZIndex = 101
LoadingTitle.TextTransparency = 1

local LoadingSubtitle = Instance.new("TextLabel")
LoadingSubtitle.Name = "LoadingSubtitle"
LoadingSubtitle.Parent = LoadingOverlay
LoadingSubtitle.BackgroundTransparency = 1
LoadingSubtitle.Position = UDim2.new(0.5, 0, 0.38, 0)
LoadingSubtitle.Size = UDim2.new(0.8, 0, 0, 20)
LoadingSubtitle.AnchorPoint = Vector2.new(0.5, 0.5)
LoadingSubtitle.Font = Enum.Font.GothamBold
LoadingSubtitle.Text = "MADE BY TEAM KS"
LoadingSubtitle.TextColor3 = Color3.fromRGB(100, 100, 100)
LoadingSubtitle.TextSize = 11
LoadingSubtitle.ZIndex = 101
LoadingSubtitle.TextTransparency = 1

local ProgressBarBg = Instance.new("Frame")
ProgressBarBg.Name = "ProgressBarBg"
ProgressBarBg.Parent = LoadingOverlay
ProgressBarBg.BackgroundColor3 = Color3.fromRGB(25, 25, 25)
ProgressBarBg.Position = UDim2.new(0.5, 0, 0.52, 0)
ProgressBarBg.Size = UDim2.new(0.6, 0, 0, 8)
ProgressBarBg.AnchorPoint = Vector2.new(0.5, 0.5)
ProgressBarBg.BorderSizePixel = 0
ProgressBarBg.ZIndex = 101
ProgressBarBg.BackgroundTransparency = 1

local ProgressBarBgCorner = Instance.new("UICorner")
ProgressBarBgCorner.CornerRadius = UDim.new(0, 4)
ProgressBarBgCorner.Parent = ProgressBarBg

local ProgressBarBgStroke = Instance.new("UIStroke")
ProgressBarBgStroke.Color = Color3.fromRGB(60, 60, 60)
ProgressBarBgStroke.Thickness = 1
ProgressBarBgStroke.Transparency = 1
ProgressBarBgStroke.Parent = ProgressBarBg

local ProgressBarFill = Instance.new("Frame")
ProgressBarFill.Name = "ProgressBarFill"
ProgressBarFill.Parent = ProgressBarBg
ProgressBarFill.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
ProgressBarFill.Size = UDim2.new(0, 0, 1, 0)
ProgressBarFill.Position = UDim2.new(0, 0, 0, 0)
ProgressBarFill.BorderSizePixel = 0
ProgressBarFill.ZIndex = 102

local ProgressBarFillCorner = Instance.new("UICorner")
ProgressBarFillCorner.CornerRadius = UDim.new(0, 4)
ProgressBarFillCorner.Parent = ProgressBarFill

local LoadingStatusText = Instance.new("TextLabel")
LoadingStatusText.Name = "LoadingStatusText"
LoadingStatusText.Parent = LoadingOverlay
LoadingStatusText.BackgroundTransparency = 1
LoadingStatusText.Position = UDim2.new(0.5, 0, 0.62, 0)
LoadingStatusText.Size = UDim2.new(0.8, 0, 0, 22)
LoadingStatusText.AnchorPoint = Vector2.new(0.5, 0.5)
LoadingStatusText.Font = Enum.Font.GothamBold
LoadingStatusText.Text = "Initializing..."
LoadingStatusText.TextColor3 = Color3.fromRGB(180, 180, 180)
LoadingStatusText.TextSize = 13
LoadingStatusText.ZIndex = 101
LoadingStatusText.TextTransparency = 1

local LoadingPercent = Instance.new("TextLabel")
LoadingPercent.Name = "LoadingPercent"
LoadingPercent.Parent = LoadingOverlay
LoadingPercent.BackgroundTransparency = 1
LoadingPercent.Position = UDim2.new(0.5, 0, 0.72, 0)
LoadingPercent.Size = UDim2.new(0.8, 0, 0, 18)
LoadingPercent.AnchorPoint = Vector2.new(0.5, 0.5)
LoadingPercent.Font = Enum.Font.GothamBlack
LoadingPercent.Text = "0%"
LoadingPercent.TextColor3 = Color3.fromRGB(255, 255, 255)
LoadingPercent.TextSize = 16
LoadingPercent.ZIndex = 101
LoadingPercent.TextTransparency = 1

-- ============================================
-- CONTENT CONTAINER
-- ============================================

local ContentContainer = Instance.new("Frame")
ContentContainer.Name = "ContentContainer"
ContentContainer.Parent = MainFrame
ContentContainer.BackgroundTransparency = 1
ContentContainer.Size = UDim2.new(1, 0, 1, 0)
ContentContainer.Position = UDim2.new(0, 0, 0, 0)
ContentContainer.Visible = false
ContentContainer.ZIndex = 1

local TopBar = Instance.new("Frame")
TopBar.Size = UDim2.new(1, 0, 0, 25)
TopBar.BackgroundTransparency = 1
TopBar.Parent = ContentContainer

local Title = Instance.new("TextLabel")
Title.Text = "KS-Rivals HUB2 - Integrated"
Title.Font = Enum.Font.Code
Title.TextColor3 = Color3.fromRGB(255, 255, 255)
Title.TextSize = 13
Title.Size = UDim2.new(1, -60, 1, 0)
Title.Position = UDim2.new(0, 10, 0, 0)
Title.TextXAlignment = Enum.TextXAlignment.Left
Title.BackgroundTransparency = 1
Title.Parent = TopBar

local CloseBtn = Instance.new("TextButton")
CloseBtn.Size = UDim2.new(0, 25, 0, 25)
CloseBtn.Position = UDim2.new(1, -25, 0, 0)
CloseBtn.BackgroundTransparency = 1
CloseBtn.Text = "X"
CloseBtn.TextColor3 = Color3.fromRGB(255, 255, 255)
CloseBtn.Font = Enum.Font.Code
CloseBtn.TextSize = 15
CloseBtn.Parent = TopBar

local TabContainer = Instance.new("Frame")
TabContainer.Size = UDim2.new(1, 0, 0, 30)
TabContainer.Position = UDim2.new(0, 0, 0, 25)
TabContainer.BackgroundTransparency = 1
TabContainer.Parent = ContentContainer

local function CreateTabBtn(name, pos)
    local btn = Instance.new("TextButton")
    btn.Text = "  " .. name .. "  "
    btn.Font = Enum.Font.Code
    btn.TextColor3 = Color3.fromRGB(150, 150, 150)
    btn.TextSize = 14
    btn.BackgroundTransparency = 1
    btn.Size = UDim2.new(0, 80, 1, 0)
    btn.Position = pos
    btn.Parent = TabContainer
    return btn
end

local AimTab = CreateTabBtn("main", UDim2.new(0, 0, 0, 0))
local VisualTab = CreateTabBtn("visuals", UDim2.new(0, 85, 0, 0))

local Content = Instance.new("Frame")
Content.Size = UDim2.new(1, -20, 1, -65)
Content.Position = UDim2.new(0, 10, 0, 55)
Content.BackgroundTransparency = 1
Content.Parent = ContentContainer

local AimPage = Instance.new("Frame")
AimPage.Size = UDim2.new(1, 0, 1, 0)
AimPage.BackgroundTransparency = 1
AimPage.Visible = true
AimPage.Parent = Content

local VisualPage = Instance.new("Frame")
VisualPage.Size = UDim2.new(1, 0, 1, 0)
VisualPage.BackgroundTransparency = 1
VisualPage.Visible = false
VisualPage.Parent = Content

-- ============================================
-- HELPER FUNCTION: Check if element should be excluded
-- ============================================

local function IsToggleCheckInner(element)
    for _, checkInner in ipairs(ToggleCheckInners) do
        if element == checkInner then
            return true
        end
    end
    return false
end

-- ============================================
-- HUB ANIMATION FUNCTIONS
-- ============================================

local function AnimateHubOpen()
    if HUB_ANIMATION_PLAYING then return end
    HUB_ANIMATION_PLAYING = true
    
    HideKeybindInfo()
    
    MainFrame.Size = UDim2.new(0, 480, 0, 0)
    MainFrame.Position = UDim2.new(0.5, -240, 0.5, 0)
    MainFrame.BackgroundTransparency = 1
    MainStroke.Transparency = 1
    ContentContainer.Visible = false
    MainFrame.Visible = true
    
    local openTween1 = TweenService:Create(MainFrame, TweenInfo.new(0.25, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {
        Size = UDim2.new(0, 480, 0, 320),
        Position = UDim2.new(0.5, -240, 0.5, -160),
        BackgroundTransparency = 0
    })
    
    local strokeTween = TweenService:Create(MainStroke, TweenInfo.new(0.25, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {
        Transparency = 0
    })
    
    openTween1:Play()
    strokeTween:Play()
    
    openTween1.Completed:Connect(function()
        ContentContainer.Visible = true
        
        for _, child in ipairs(ContentContainer:GetDescendants()) do
            if IsToggleCheckInner(child) then
                continue
            end
            
            if child:IsA("TextLabel") or child:IsA("TextButton") then
                child.TextTransparency = 1
                TweenService:Create(child, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
                    TextTransparency = 0
                }):Play()
            end
            
            if child:IsA("Frame") and child.BackgroundTransparency < 1 and not IsToggleCheckInner(child) then
                local originalTransparency = child.BackgroundTransparency
                child.BackgroundTransparency = 1
                TweenService:Create(child, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
                    BackgroundTransparency = originalTransparency
                }):Play()
            end
        end
        
        task.wait(0.2)
        HUB_ANIMATION_PLAYING = false
    end)
end

local function AnimateHubClose(callback)
    if HUB_ANIMATION_PLAYING then return end
    HUB_ANIMATION_PLAYING = true
    
    for _, child in ipairs(ContentContainer:GetDescendants()) do
        if IsToggleCheckInner(child) then
            continue
        end
        
        if child:IsA("TextLabel") or child:IsA("TextButton") then
            TweenService:Create(child, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
                TextTransparency = 1
            }):Play()
        end
        if child:IsA("Frame") and child.BackgroundTransparency < 1 then
            TweenService:Create(child, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
                BackgroundTransparency = 1
            }):Play()
        end
    end
    
    task.wait(0.15)
    ContentContainer.Visible = false
    
    local closeTween = TweenService:Create(MainFrame, TweenInfo.new(0.2, Enum.EasingStyle.Quart, Enum.EasingDirection.In), {
        Size = UDim2.new(0, 480, 0, 0),
        Position = UDim2.new(0.5, -240, 0.5, 0),
        BackgroundTransparency = 1
    })
    
    local strokeTween = TweenService:Create(MainStroke, TweenInfo.new(0.2, Enum.EasingStyle.Quart, Enum.EasingDirection.In), {
        Transparency = 1
    })
    
    closeTween:Play()
    strokeTween:Play()
    
    closeTween.Completed:Connect(function()
        MainFrame.Visible = false
        MainFrame.Size = UDim2.new(0, 480, 0, 320)
        MainFrame.Position = UDim2.new(0.5, -240, 0.5, -160)
        MainFrame.BackgroundTransparency = 0
        MainStroke.Transparency = 0
        HUB_ANIMATION_PLAYING = false
        
        ShowKeybindInfo()
        
        if callback then callback() end
    end)
end

local function UpdateLoadingProgress(percent, statusText)
    LoadingPercent.Text = math.floor(percent) .. "%"
    if statusText then LoadingStatusText.Text = statusText end
    TweenService:Create(ProgressBarFill, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
        Size = UDim2.new(percent / 100, 0, 1, 0)
    }):Play()
end

local function StartLoadingAnimation()
    MainFrame.Size = UDim2.new(0, 480, 0, 320)
    MainFrame.Position = UDim2.new(0.5, -240, 0.5, -160)
    MainFrame.BackgroundTransparency = 1
    MainStroke.Transparency = 1
    LoadingOverlay.Visible = true
    ContentContainer.Visible = false
    MainFrame.Visible = true
    
    TweenService:Create(MainFrame, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 0}):Play()
    TweenService:Create(MainStroke, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Transparency = 0}):Play()
    task.wait(0.2)
    TweenService:Create(LoadingTitle, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {TextTransparency = 0}):Play()
    task.wait(0.1)
    TweenService:Create(LoadingSubtitle, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {TextTransparency = 0}):Play()
    task.wait(0.1)
    TweenService:Create(ProgressBarBg, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {BackgroundTransparency = 0}):Play()
    TweenService:Create(ProgressBarBgStroke, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Transparency = 0}):Play()
    TweenService:Create(LoadingStatusText, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {TextTransparency = 0}):Play()
    TweenService:Create(LoadingPercent, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {TextTransparency = 0}):Play()
end

local function EndLoadingAnimation(callback)
    LoadingStatusText.Text = "Loading Complete!"
    LoadingStatusText.TextColor3 = Color3.fromRGB(100, 255, 100)
    task.wait(0.6)
    TweenService:Create(LoadingTitle, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {TextTransparency = 1}):Play()
    TweenService:Create(LoadingSubtitle, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {TextTransparency = 1}):Play()
    TweenService:Create(ProgressBarBg, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {BackgroundTransparency = 1}):Play()
    TweenService:Create(ProgressBarBgStroke, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Transparency = 1}):Play()
    TweenService:Create(ProgressBarFill, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {BackgroundTransparency = 1}):Play()
    TweenService:Create(LoadingStatusText, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {TextTransparency = 1}):Play()
    TweenService:Create(LoadingPercent, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {TextTransparency = 1}):Play()
    task.wait(0.4)
    LoadingOverlay.Visible = false
    ContentContainer.Visible = true
    for _, child in ipairs(ContentContainer:GetDescendants()) do
        if IsToggleCheckInner(child) then continue end
        if child:IsA("TextLabel") or child:IsA("TextButton") then
            child.TextTransparency = 1
            TweenService:Create(child, TweenInfo.new(0.25, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {TextTransparency = 0}):Play()
        end
        if child:IsA("Frame") and child.BackgroundTransparency < 1 then
            local originalBgTransparency = child.BackgroundTransparency
            child.BackgroundTransparency = 1
            TweenService:Create(child, TweenInfo.new(0.25, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {BackgroundTransparency = originalBgTransparency}):Play()
        end
    end
    task.wait(0.3)
    LOADING_COMPLETE = true
    if callback then callback() end
end

-- ============================================
-- NO RECOIL LOGIC
-- ============================================

local function initNoRecoilModules()
    pcall(function()
        local PlayerScripts = LocalPlayer:WaitForChild("PlayerScripts", 5)
        if not PlayerScripts then return end
        
        for _, module in pairs(PlayerScripts:GetDescendants()) do
            if module:IsA("ModuleScript") and module.Name == "Gun" then
                nrGunModule = require(module)
                break
            end
        end
        
        if nrGunModule then
            nrOriginalRecoil = nrGunModule._Recoil
            nrOriginalStartShooting = nrGunModule.StartShooting
            nrOriginal_LocalTracers = nrGunModule._LocalTracers
        end
        
        local ItemLibraryModule = ReplicatedStorage:WaitForChild("Modules", 5)
        if ItemLibraryModule then
            ItemLibraryModule = ItemLibraryModule:WaitForChild("ItemLibrary", 5)
            if ItemLibraryModule then
                nrItemLibrary = require(ItemLibraryModule)
            end
        end
    end)
end

local function enableNoRecoil()
    if not nrGunModule then
        initNoRecoilModules()
        if not nrGunModule then
            createNotification("NoRecoil: Gun module not found")
            return
        end
    end
    
    pcall(function()
        nrGunModule._Recoil = function(self, multiplier)
            return
        end
        
        nrGunModule.StartShooting = function(self, arg2, arg3)
            local success, event, cameraData, isAiming, extra1, extra2 = nrOriginalStartShooting(self, arg2, arg3)
            if success then
                return success, event, cameraData, true, extra1, extra2
            end
            return success, event, cameraData, isAiming, extra1, extra2
        end
        
        nrGunModule._LocalTracers = function(self, isAiming, arg3)
            return nrOriginal_LocalTracers(self, true, arg3)
        end
        
        if nrItemLibrary and nrItemLibrary.Items then
            for weaponName, weaponData in pairs(nrItemLibrary.Items) do
                if weaponData.Type == "Gun" then
                    weaponData.ShootRecoil = 0
                    weaponData.AimSpreadMultiplier = 0
                end
            end
        end
        
        if not nrCrosshairProtected then
            task.spawn(function()
                local playerGui = LocalPlayer:WaitForChild("PlayerGui", 5)
                if not playerGui then return end
                
                local possiblePaths = {
                    "Crosshair",
                    "HUD/Crosshair",
                    "GameUI/Crosshair",
                    "Interface/Crosshair",
                    "Gameplay/Crosshair"
                }
                
                for _, path in ipairs(possiblePaths) do
                    local parts = string.split(path, "/")
                    local current = playerGui
                    
                    for _, part in ipairs(parts) do
                        current = current:FindFirstChild(part)
                        if not current then break end
                    end
                    
                    if current then
                        RunService.RenderStepped:Connect(function()
                            if not NO_RECOIL_ENABLED then return end
                            if current then
                                current.Visible = true
                                for _, descendant in ipairs(current:GetDescendants()) do
                                    if descendant:IsA("GuiObject") then
                                        if descendant:IsA("ImageLabel") or descendant:IsA("ImageButton") then
                                            if descendant.ImageTransparency > 0.9 then
                                                descendant.ImageTransparency = 0
                                            end
                                        end
                                        if descendant:IsA("TextLabel") or descendant:IsA("TextButton") then
                                            if descendant.TextTransparency > 0.9 then
                                                descendant.TextTransparency = 0
                                            end
                                        end
                                        descendant.Visible = true
                                    end
                                end
                            end
                        end)
                        nrCrosshairProtected = true
                        break
                    end
                end
            end)
        end
    end)
    
    createNotification("No Recoil Enabled")
end

local function disableNoRecoil()
    if not nrGunModule or not nrOriginalRecoil or not nrOriginalStartShooting or not nrOriginal_LocalTracers then
        createNotification("No Recoil Disabled")
        return
    end
    
    pcall(function()
        nrGunModule._Recoil = nrOriginalRecoil
        nrGunModule.StartShooting = nrOriginalStartShooting
        nrGunModule._LocalTracers = nrOriginal_LocalTracers
    end)
    
    createNotification("No Recoil Disabled")
end

-- ============================================
-- THIRD PERSON CAMERA LOGIC (Updated with Mouse Wheel Zoom)
-- ============================================

local function updateThirdPersonCamera()
    local character = LocalPlayer.Character
    if character and character:FindFirstChild("HumanoidRootPart") then
        local rootPart = character.HumanoidRootPart
        local currentRotation = Camera.CFrame.Rotation
        local targetPosition = rootPart.Position + Vector3.new(0, heightOffset, 0)
        local relativeOffset = Vector3.new(sideOffset, 0, distance)
        local cameraPosition = targetPosition + (currentRotation * relativeOffset)
        Camera.CFrame = currentRotation + cameraPosition
        
        for _, part in pairs(character:GetChildren()) do
            if part:IsA("BasePart") then
                part.LocalTransparencyModifier = 0
            end
        end
    end
end

local function enableThirdPerson()
    if thirdPersonConnection then
        thirdPersonConnection:Disconnect()
    end
    if thirdPersonWheelConnection then
        thirdPersonWheelConnection:Disconnect()
    end
    
    RunService:UnbindFromRenderStep("ThirdPersonCamera")
    RunService:BindToRenderStep("ThirdPersonCamera", Enum.RenderPriority.Camera.Value + 1, updateThirdPersonCamera)
    thirdPersonConnection = true
    
    thirdPersonWheelConnection = UserInputService.InputChanged:Connect(function(input, gameProcessed)
        if THIRD_PERSON_ENABLED and input.UserInputType == Enum.UserInputType.MouseWheel then
            local scrollDelta = input.Position.Z
            distance = math.clamp(distance - (scrollDelta * ZOOM_SPEED), MIN_DISTANCE, MAX_DISTANCE)
        end
    end)
    
    createNotification("Third Person Enabled")
end

local function disableThirdPerson()
    RunService:UnbindFromRenderStep("ThirdPersonCamera")
    if thirdPersonConnection then
        thirdPersonConnection = nil
    end
    if thirdPersonWheelConnection then
        thirdPersonWheelConnection:Disconnect()
        thirdPersonWheelConnection = nil
    end
    Camera.CameraType = Enum.CameraType.Custom
    distance = 12
    createNotification("Third Person Disabled")
end

-- ============================================
-- NO COOL TIME LOGIC
-- ============================================

local function removeCooldowns()
    local currentTime = tick()
    if currentTime - lastCooldownRemoval < COOLDOWN_REMOVAL_INTERVAL then return noCoolTimeApplied end
    local success, result = pcall(function()
        if not weaponDataCache then
            local modulesFolder = ReplicatedStorage:FindFirstChild("Modules")
            if not modulesFolder then return false end
            local possibleNames = {"ItemLibrary", "WeaponStats", "Items", "WeaponData", "Weapons"}
            local weaponModule = nil
            for _, name in ipairs(possibleNames) do
                local found = modulesFolder:FindFirstChild(name)
                if found then weaponModule = found break end
            end
            if not weaponModule then return false end
            weaponDataCache = require(weaponModule)
        end
        if not weaponDataCache.Items then return false end
        for _, stats in pairs(weaponDataCache.Items) do
            if stats.ShootCooldown then stats.ShootCooldown = 0 end
            if stats.ShootBurstCooldown then stats.ShootBurstCooldown = 0 end
            if stats.AttackCooldown then stats.AttackCooldown = 0 end
            if stats.HeavyAttackCooldown then stats.HeavyAttackCooldown = 0 end
            if stats.Cooldown then stats.Cooldown = 0 end
            if stats.DeflectCooldown then stats.DeflectCooldown = 0 end
            if stats.DashCooldown then stats.DashCooldown = 0 end
            if stats.BuildCooldown then stats.BuildCooldown = 0 end
            if stats.SpinCooldown then stats.SpinCooldown = 0 end
            if stats.AirblastCooldown then stats.AirblastCooldown = 0 end
            if stats.QuickShotCooldown then stats.QuickShotCooldown = 0 end
            if stats.TransitionCooldown then stats.TransitionCooldown = 0 end
            if stats.ReloadTime then stats.ReloadTime = 0.01 end
        end
        lastCooldownRemoval = currentTime
        return true
    end)
    noCoolTimeApplied = success and result
    return noCoolTimeApplied
end

local function hookCharacterForNoCoolTime()
    local character = LocalPlayer.Character
    if not character then return end
    character.ChildAdded:Connect(function(child)
        if child:IsA("Tool") and NO_COOL_TIME_ENABLED then
            task.wait(0.05)
            removeCooldowns()
        end
    end)
end

-- ============================================
-- RAGE BOT LOGIC (Team Check Integrated)
-- ============================================

local function rbUpdateCharacterRefs()
    rbChar = LocalPlayer.Character
    if rbChar then
        rbHrp = rbChar:WaitForChild("HumanoidRootPart", 5)
        rbHum = rbChar:WaitForChild("Humanoid", 5)
    end
end

local function rbFindNearestPlayer()
    if not rbHrp then return nil end
    local nearest = nil
    local minDist = math.huge
    for _, p in pairs(Players:GetPlayers()) do
        if p ~= LocalPlayer and p.Character and isEnemy(p) then
            local pHrp = p.Character:FindFirstChild("HumanoidRootPart")
            local pH = p.Character:FindFirstChild("Humanoid")
            if pHrp and pH and pH.Health > 0 then
                local dist = (rbHrp.Position - pHrp.Position).Magnitude
                if dist < minDist then
                    minDist = dist
                    nearest = p
                end
            end
        end
    end
    return nearest
end

local function rbMainLoop()
    if not rbEnabled then return end
    rbTarget = rbFindNearestPlayer()
    if rbTarget and rbTarget.Character and rbHrp then
        local tHrp = rbTarget.Character:FindFirstChild("HumanoidRootPart")
        local tH = rbTarget.Character:FindFirstChild("Humanoid")
        if tHrp and tH and tH.Health > 0 then
            local offsetX = math.random(-8, 8)
            local offsetY = math.random(6, 12)
            local offsetZ = math.random(-8, 8)
            local targetPos = tHrp.Position + Vector3.new(offsetX, offsetY, offsetZ)
            rbHrp.CFrame = CFrame.new(targetPos)
            local time = tick() * 25
            local rotX = math.rad(math.sin(time * 3.7) * 360)
            local rotY = math.rad(math.cos(time * 4.3) * 360)
            local rotZ = math.rad(math.sin(time * 5.1) * 360)
            rbHrp.CFrame = rbHrp.CFrame * CFrame.Angles(rotX, rotY, rotZ)
            rbHrp.Velocity = Vector3.new(math.random(-50, 50), math.random(-50, 50), math.random(-50, 50))
        end
    end
end

LocalPlayer.CharacterAdded:Connect(function(newChar)
    if rbEnabled then
        rbEnabled = false
        if rbConnection then rbConnection:Disconnect() rbConnection = nil end
    end
    rbUpdateCharacterRefs()
end)

-- ============================================
-- FULL AUTO LOGIC (Team Check Integrated)
-- ============================================

local function faFindClosestPlayer()
    if not FULL_AUTO_ENABLED then return nil end
    local character = LocalPlayer.Character
    if not character or not character:FindFirstChild("HumanoidRootPart") then return nil end
    local myPos = character.HumanoidRootPart.Position
    local closestPlayer = nil
    local shortestDistance = math.huge
    for _, player in pairs(Players:GetPlayers()) do
        if player ~= LocalPlayer and player.Character and isEnemy(player) then
            local theirRoot = player.Character:FindFirstChild("HumanoidRootPart")
            if theirRoot then
                local distance = (myPos - theirRoot.Position).Magnitude
                if distance < shortestDistance then
                    shortestDistance = distance
                    closestPlayer = player
                end
            end
        end
    end
    return closestPlayer
end

local function faPressR()
    if not FULL_AUTO_ENABLED then return end
    VirtualInputManager:SendKeyEvent(true, Enum.KeyCode.R, false, game)
    task.wait(0.05)
    VirtualInputManager:SendKeyEvent(false, Enum.KeyCode.R, false, game)
end

local function faDisableGravity()
    if faGravityConnection then faGravityConnection:Disconnect() end
    faGravityConnection = RunService.RenderStepped:Connect(function()
        if not FULL_AUTO_ENABLED then 
            if faGravityConnection then faGravityConnection:Disconnect() faGravityConnection = nil end
            return 
        end
        local character = LocalPlayer.Character
        if character then
            local root = character:FindFirstChild("HumanoidRootPart")
            if root then
                local bodyVelocity = root:FindFirstChild("FA_NoGravity")
                if not bodyVelocity then
                    bodyVelocity = Instance.new("BodyVelocity")
                    bodyVelocity.Name = "FA_NoGravity"
                    bodyVelocity.MaxForce = Vector3.new(0, math.huge, 0)
                    bodyVelocity.Velocity = Vector3.new(0, 0, 0)
                    bodyVelocity.Parent = root
                end
            end
        end
    end)
end

local function faEnableGravity()
    if faGravityConnection then faGravityConnection:Disconnect() faGravityConnection = nil end
    local character = LocalPlayer.Character
    if character then
        local root = character:FindFirstChild("HumanoidRootPart")
        if root then
            local bodyVelocity = root:FindFirstChild("FA_NoGravity")
            if bodyVelocity then bodyVelocity:Destroy() end
        end
    end
end

local function faStartSpinning()
    if faSpinConnection then faSpinConnection:Disconnect() end
    faSpinConnection = RunService.RenderStepped:Connect(function()
        if not FULL_AUTO_ENABLED then 
            if faSpinConnection then faSpinConnection:Disconnect() faSpinConnection = nil end
            return 
        end
        if faIsRetreating then return end
        local character = LocalPlayer.Character
        if character and character:FindFirstChild("HumanoidRootPart") then
            local root = character.HumanoidRootPart
            root.CFrame = root.CFrame * CFrame.Angles(math.rad(math.random(-30, 30)), math.rad(math.random(0, 360)), math.rad(math.random(-30, 30)))
        end
    end)
end

local function faStartCameraLock()
    if faCameraConnection then faCameraConnection:Disconnect() end
    faCameraConnection = RunService.RenderStepped:Connect(function()
        if not FULL_AUTO_ENABLED then 
            Camera.CameraType = Enum.CameraType.Custom
            if faCameraConnection then faCameraConnection:Disconnect() faCameraConnection = nil end
            return 
        end
        if faIsRetreating then return end
        local character = LocalPlayer.Character
        local myRoot = character and character:FindFirstChild("HumanoidRootPart")
        if faTargetPlayer and faTargetPlayer.Character and myRoot then
            local targetHead = faTargetPlayer.Character:FindFirstChild("Head")
            if targetHead then
                Camera.CameraType = Enum.CameraType.Scriptable
                local cameraPosition = myRoot.Position + Vector3.new(0, 5, 0)
                Camera.CFrame = CFrame.new(cameraPosition, targetHead.Position)
            end
        end
    end)
end

local function faIsTargetInRange()
    if not FULL_AUTO_ENABLED then return false end
    if not faTargetPlayer or not faTargetPlayer.Character then return false end
    local character = LocalPlayer.Character
    local myRoot = character and character:FindFirstChild("HumanoidRootPart")
    local targetRoot = faTargetPlayer.Character:FindFirstChild("HumanoidRootPart")
    if not myRoot or not targetRoot then return false end
    local distance = (myRoot.Position - targetRoot.Position).Magnitude
    local verticalDistance = math.abs(myRoot.Position.Y - targetRoot.Position.Y)
    return distance <= 30 and verticalDistance <= 25
end

local function faStartAutoClick()
    if faAutoClickConnection then faAutoClickConnection:Disconnect() end
    faAutoClickConnection = RunService.RenderStepped:Connect(function()
        if not FULL_AUTO_ENABLED then 
            if faAutoClickConnection then faAutoClickConnection:Disconnect() faAutoClickConnection = nil end
            return 
        end
        if faAutoClickPaused or faIsRetreating then return end
        if MainFrame.Visible then return end
        if not faIsTargetInRange() then return end
        local cur = tick()
        if cur - faLastAutoClickTime >= FA_CLICK_INTERVAL then
            faLastAutoClickTime = cur
            pcall(function() mouse1click() end)
        end
    end)
end

local function faPerformRetreat(targetRoot)
    if not FULL_AUTO_ENABLED then return end
    if faRetreatLock then return end
    faRetreatLock = true
    faIsRetreating = true
    faAutoClickPaused = true
    local character = LocalPlayer.Character
    local root = character and character:FindFirstChild("HumanoidRootPart")
    if not root or not targetRoot then
        faIsRetreating = false
        faAutoClickPaused = false
        faRetreatLock = false
        return
    end
    Camera.CameraType = Enum.CameraType.Custom
    root.CFrame = CFrame.new(targetRoot.Position + Vector3.new(0, 10000, 0))
    task.wait(0.2)
    faPressR()
    task.wait(2)
    faAutoClickPaused = false
    faIsRetreating = false
    faLastRetreatTime = tick()
    faIsAbove = false
    faRetreatLock = false
    local newHumanoid = LocalPlayer.Character and LocalPlayer.Character:FindFirstChild("Humanoid")
    if newHumanoid then faLastHealth = newHumanoid.Health end
    local newTargetChar = faTargetPlayer and faTargetPlayer.Character
    local newTargetHum = newTargetChar and newTargetChar:FindFirstChild("Humanoid")
    if newTargetHum then faTargetLastHealth = newTargetHum.Health end
end

local function faMainLoop()
    faLoopActive = true
    while FULL_AUTO_ENABLED do
        task.wait(0.1)
        if not FULL_AUTO_ENABLED then break end
        if not faTargetPlayer or not faTargetPlayer.Character then
            faTargetPlayer = faFindClosestPlayer()
            if not faTargetPlayer then task.wait(0.5) continue end
            local targetHum = faTargetPlayer.Character:FindFirstChild("Humanoid")
            if targetHum then 
                faTargetLastHealth = targetHum.Health
                faTargetMaxHealth = targetHum.MaxHealth
            end
        end
        local character = LocalPlayer.Character
        local humanoid = character and character:FindFirstChild("Humanoid")
        local root = character and character:FindFirstChild("HumanoidRootPart")
        if not character or not humanoid or not root then task.wait(0.5) continue end
        local targetChar = faTargetPlayer.Character
        local targetRoot = targetChar and targetChar:FindFirstChild("HumanoidRootPart")
        local targetHumanoid = targetChar and targetChar:FindFirstChild("Humanoid")
        if not targetRoot or not targetHumanoid then 
            faTargetPlayer = nil 
            task.wait(0.5)
            continue 
        end
        local currentHealth = humanoid.Health
        local currentTime = tick()
        if not faRetreatLock then
            if currentHealth < faLastHealth and faLastHealth > 0 then
                faPerformRetreat(targetRoot)
            elseif not faIsRetreating and (currentTime - faLastRetreatTime >= FA_RETREAT_INTERVAL) then
                faPerformRetreat(targetRoot)
            elseif not faIsAbove and not faIsRetreating then
                root.CFrame = CFrame.new(targetRoot.Position + Vector3.new(0, 10000, 0))
                task.wait(math.random(10, 20) / 10)
                faIsAbove = true
            elseif not faIsRetreating then
                root.CFrame = CFrame.new(targetRoot.Position + Vector3.new(0, 15, 0))
                if faIsFirstTeleport then
                    task.wait(2)
                    faIsFirstTeleport = false
                end
                faLastHealth = currentHealth
            end
        end
    end
    faLoopActive = false
end

LocalPlayer.CharacterAdded:Connect(function(character)
    if FULL_AUTO_ENABLED then
        task.wait(1)
        local humanoid = character:WaitForChild("Humanoid")
        faLastHealth = humanoid.Health
        faIsAbove = false
        faIsFirstTeleport = true
        faTargetPlayer = nil
        faAutoClickPaused = false
        faIsRetreating = false
        faRetreatLock = false
        faLastRetreatTime = tick()
        faDisableGravity()
        faStartSpinning()
        faStartCameraLock()
        faStartAutoClick()
        if not faLoopActive then task.spawn(faMainLoop) end
    end
end)

-- ============================================
-- UI COMPONENTS
-- ============================================

local UIHandlers = {}

local function ShowTab(tabName)
    if tabName == "Aim" then
        AimPage.Visible = true
        VisualPage.Visible = false
        AimTab.TextColor3 = Color3.fromRGB(255, 255, 255)
        VisualTab.TextColor3 = Color3.fromRGB(130, 130, 130)
    else
        AimPage.Visible = false
        VisualPage.Visible = true
        AimTab.TextColor3 = Color3.fromRGB(130, 130, 130)
        VisualTab.TextColor3 = Color3.fromRGB(255, 255, 255)
    end
end

AimTab.MouseButton1Click:Connect(function() ShowTab("Aim") end)
VisualTab.MouseButton1Click:Connect(function() ShowTab("Visual") end)

local function CreateToggle(name, parent, callback, configType, defaultValue)
    local container = Instance.new("Frame")
    container.Size = UDim2.new(1, 0, 0, 24)
    container.BackgroundTransparency = 1
    container.Parent = parent

    local box = Instance.new("TextButton")
    box.Size = UDim2.new(0, 14, 0, 14)
    box.Position = UDim2.new(0, 5, 0.5, -7)
    box.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
    box.Text = ""
    box.BorderSizePixel = 0
    box.Parent = container

    local boxStroke = Instance.new("UIStroke")
    boxStroke.Color = Color3.fromRGB(255, 255, 255)
    boxStroke.Thickness = 1
    boxStroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
    boxStroke.Parent = box

    local checkInner = Instance.new("Frame")
    checkInner.Name = "CheckInner"
    checkInner.Size = UDim2.new(1, -4, 1, -4)
    checkInner.Position = UDim2.new(0, 2, 0, 2)
    checkInner.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    checkInner.Visible = (defaultValue == true)
    checkInner.BorderSizePixel = 0
    checkInner.Parent = box
    table.insert(ToggleCheckInners, checkInner)

    local label = Instance.new("TextButton")
    label.Size = UDim2.new(1, -100, 1, 0)
    label.Position = UDim2.new(0, 30, 0, 0)
    label.BackgroundTransparency = 1
    label.Text = name
    label.Font = Enum.Font.Code
    label.TextColor3 = Color3.fromRGB(255, 255, 255)
    label.TextSize = 13
    label.TextXAlignment = Enum.TextXAlignment.Left
    label.Parent = container

    local enabled = defaultValue or false
    local function setEnabled(val)
        enabled = val
        checkInner.Visible = val
        if callback then callback(val) end
        saveConfig()
    end
    local function toggle() setEnabled(not enabled) end
    box.MouseButton1Click:Connect(toggle)
    label.MouseButton1Click:Connect(toggle)

    if configType then
        local configBtn = Instance.new("TextButton")
        configBtn.Size = UDim2.new(0, 60, 0, 18)
        configBtn.Position = UDim2.new(1, -65, 0.5, -9)
        configBtn.BackgroundColor3 = Color3.fromRGB(15, 15, 15)
        configBtn.Font = Enum.Font.Code
        configBtn.TextColor3 = Color3.fromRGB(255, 255, 255)
        configBtn.TextSize = 11
        configBtn.BorderSizePixel = 0
        configBtn.Parent = container
        
        local configStroke = Instance.new("UIStroke")
        configStroke.Color = Color3.fromRGB(255, 255, 255)
        configStroke.Thickness = 1
        configStroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
        configStroke.Parent = configBtn

        if configType == "Keybind" then
            configBtn.Text = "[" .. formatKeyName(AIM_LOCK_KEY) .. "]"
            configBtn.MouseButton1Click:Connect(function()
                configBtn.Text = "..."
                local inputConn
                inputConn = UserInputService.InputBegan:Connect(function(input)
                    local newBind = input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode or input.UserInputType
                    if newBind ~= Enum.UserInputType.MouseMovement then
                        AIM_LOCK_KEY = newBind
                        configBtn.Text = "[" .. formatKeyName(newBind) .. "]"
                        UpdateKeybindInfo()
                        saveConfig()
                        inputConn:Disconnect()
                    end
                end)
            end)
        elseif configType == "RageBotKeybind" then
            configBtn.Text = "[" .. formatKeyName(RAGE_BOT_KEY) .. "]"
            configBtn.MouseButton1Click:Connect(function()
                configBtn.Text = "..."
                local inputConn
                inputConn = UserInputService.InputBegan:Connect(function(input)
                    if input.UserInputType == Enum.UserInputType.Keyboard then
                        RAGE_BOT_KEY = input.KeyCode
                        configBtn.Text = "[" .. formatKeyName(input.KeyCode) .. "]"
                        UpdateKeybindInfo()
                        saveConfig()
                        inputConn:Disconnect()
                    end
                end)
            end)
        elseif configType == "AimMode" then
            configBtn.Text = AUTO_AIM_MODE
            configBtn.MouseButton1Click:Connect(function()
                AUTO_AIM_MODE = (AUTO_AIM_MODE == "Silent") and "Aim" or "Silent"
                configBtn.Text = AUTO_AIM_MODE
                if AUTO_AIM_ENABLED then
                    if AUTO_AIM_MODE == "Silent" then
                        UIHandlers.UpdateSilentAim(true)
                        UIHandlers.UpdateAimLock(false)
                    else
                        UIHandlers.UpdateSilentAim(false)
                        UIHandlers.UpdateAimLock(true)
                    end
                end
                saveConfig()
            end)
        elseif configType == "MatchMode" then
            configBtn.Text = AUTO_MATCH_MODE
            local modes = {"1v1", "2v2", "3v3", "4v4", "5v5"}
            configBtn.MouseButton1Click:Connect(function()
                local currentIndex = 1
                for i, mode in ipairs(modes) do
                    if mode == AUTO_MATCH_MODE then currentIndex = i break end
                end
                local nextIndex = (currentIndex % #modes) + 1
                AUTO_MATCH_MODE = modes[nextIndex]
                configBtn.Text = AUTO_MATCH_MODE
                saveConfig()
            end)
        end
    end
    return setEnabled
end

local function CreateButton(name, parent, callback)
    local btn = Instance.new("TextButton")
    btn.Size = UDim2.new(0, 140, 0, 26)
    btn.BackgroundColor3 = Color3.fromRGB(15, 15, 15)
    btn.Text = name
    btn.Font = Enum.Font.Code
    btn.TextColor3 = Color3.fromRGB(255, 255, 255)
    btn.TextSize = 13
    btn.BorderSizePixel = 0
    btn.Parent = parent
    
    local s = Instance.new("UIStroke")
    s.Color = Color3.fromRGB(255, 255, 255)
    s.Thickness = 1
    s.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
    s.Parent = btn
    
    btn.MouseButton1Click:Connect(callback)
end

local AimList = Instance.new("UIListLayout")
AimList.Padding = UDim.new(0, 4)
AimList.Parent = AimPage

local VisualList = Instance.new("UIListLayout")
VisualList.Padding = UDim.new(0, 4)
VisualList.Parent = VisualPage

local function attemptJoinMatch()
    if not AUTO_MATCH_ENABLED then return end
    pcall(function()
        local args = { AUTO_MATCH_MODE }
        ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("Matchmaking"):WaitForChild("JoinQueue"):InvokeServer(unpack(args))
    end)
end

task.spawn(function()
    while task.wait(1) do
        if AUTO_MATCH_ENABLED then attemptJoinMatch() end
    end
end)

local function setupPlayerTracking(p)
    if p == LocalPlayer then return end
    p.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid", 10)
        if not hum then return end
        local lastH = hum.Health
        hum.HealthChanged:Connect(function(h)
            if not NOTIFICATION_ENABLED then return end
            if not isEnemy(p) then return end
            local diff = h - lastH
            if diff < 0 then
                createNotification(string.format("Hit %s -%d", p.Name, math.floor(-diff)))
            elseif diff > 0 then
                createNotification(string.format("Healing %s +%d", p.Name, math.floor(diff)))
            end
            lastH = h
        end)
        hum.Died:Connect(function()
            if NOTIFICATION_ENABLED and isEnemy(p) then createNotification("Killed " .. p.Name) end
        end)
    end)
end

local function getClosestPlayer()
    local closest, shortestDist = nil, math.huge
    for _, p in pairs(Players:GetPlayers()) do
        if p ~= LocalPlayer and isEnemy(p) and p.Character and p.Character:FindFirstChild("Head") and p.Character:FindFirstChild("Humanoid") and p.Character.Humanoid.Health > 0 then
            local head = p.Character.Head
            local _, onScreen = Camera:WorldToScreenPoint(head.Position)
            if onScreen and LocalPlayer.Character and LocalPlayer.Character:FindFirstChild("HumanoidRootPart") then
                local dist = (LocalPlayer.Character.HumanoidRootPart.Position - head.Position).Magnitude
                if dist < shortestDist then
                    shortestDist = dist
                    closest = p
                end
            end
        end
    end
    return closest
end

local function updateAimLock()
    if not isAimLockHeld or not AIM_LOCK_ENABLED then return end
    local t = getClosestPlayer()
    if t and t.Character and t.Character:FindFirstChild("Head") then
        local pos = Camera:WorldToScreenPoint(t.Character.Head.Position)
        pcall(function()
            mousemoverel(math.floor(pos.X - Mouse.X + 0.5), math.floor(pos.Y - Mouse.Y + 0.5))
        end)
    end
end

UserInputService.InputBegan:Connect(function(i, gpe)
    if not gpe and AIM_LOCK_ENABLED and (i.KeyCode == AIM_LOCK_KEY or i.UserInputType == AIM_LOCK_KEY) then
        isAimLockHeld = true
        if aimLockConnection then aimLockConnection:Disconnect() end
        aimLockConnection = RunService.RenderStepped:Connect(updateAimLock)
    elseif not gpe and RAGE_BOT_ENABLED and i.KeyCode == RAGE_BOT_KEY then
        rbEnabled = not rbEnabled
        if rbEnabled then
            rbUpdateCharacterRefs()
            if rbConnection then rbConnection:Disconnect() end
            rbConnection = RunService.Heartbeat:Connect(rbMainLoop)
            createNotification("Rage Bot Activated!")
        else
            if rbConnection then
                rbConnection:Disconnect()
                rbConnection = nil
            end
            if rbHrp then rbHrp.Velocity = Vector3.new(0, 0, 0) end
            createNotification("Rage Bot Deactivated!")
        end
    end
end)

UserInputService.InputEnded:Connect(function(i)
    if i.KeyCode == AIM_LOCK_KEY or i.UserInputType == AIM_LOCK_KEY then
        isAimLockHeld = false
        if aimLockConnection then aimLockConnection:Disconnect() aimLockConnection = nil end
    end
end)

RunService.RenderStepped:Connect(function()
    if not AUTO_AIM_ENABLED or MainFrame.Visible then return end
    local targetFound = false
    if AUTO_AIM_MODE == "Silent" then
        local t = getClosestPlayerForSilentAim()
        if t then targetFound = true end
    elseif AUTO_AIM_MODE == "Aim" then
        local target = Mouse.Target
        if target and target.Parent then
            local p = Players:GetPlayerFromCharacter(target.Parent) or Players:GetPlayerFromCharacter(target.Parent.Parent)
            if p and p ~= LocalPlayer and isEnemy(p) and p.Character and p.Character:FindFirstChild("Humanoid") and p.Character.Humanoid.Health > 0 then
                targetFound = true
            end
        end
    end
    if targetFound then
        local cur = tick()
        if cur - lastClickTime >= CLICK_INTERVAL then
            lastClickTime = cur
            pcall(function()
                VirtualInputManager:SendMouseButtonEvent(Mouse.X, Mouse.Y, 0, true, game, 0)
                task.wait(0.01)
                VirtualInputManager:SendMouseButtonEvent(Mouse.X, Mouse.Y, 0, false, game, 0)
            end)
        end
    end
end)

local function updateESPStates()
    for _, p in pairs(Players:GetPlayers()) do
        local r = p.Character and p.Character:FindFirstChild("HumanoidRootPart")
        if r then
            local g = r:FindFirstChild("ESP_Gui")
            if g then g.Enabled = ESP_ENABLED and isEnemy(p) end
            local h = r:FindFirstChild("HealthESP_Gui")
            if h then h.Enabled = HP_BAR_ENABLED and isEnemy(p) end
        end
    end
end

local function createESP(player)
    if player == LocalPlayer then return end
    local function onChar(char)
        local root = char:WaitForChild("HumanoidRootPart", 5)
        local hum = char:WaitForChild("Humanoid", 5)
        if not root or not hum then return end
        
        local bGui = Instance.new("BillboardGui", root)
        bGui.Name = "ESP_Gui"
        bGui.Size = UDim2.new(4, 0, 5.5, 0)
        bGui.AlwaysOnTop = true
        bGui.Enabled = ESP_ENABLED and isEnemy(player)
        
        -- 硫붿씤 而⑦뀒�대꼫 (�щ챸)
        local container = Instance.new("Frame", bGui)
        container.Size = UDim2.new(1, 0, 1, 0)
        container.BackgroundTransparency = 1
        
        -- 寃����� �ㅺ낸�좎쓣 媛�吏� �쇱씤 �앹꽦 �⑥닔
        local function line(s, p)
            -- 寃����� �멸낸�� (�� �먭퍖寃�)
            local outline = Instance.new("Frame", container)
            outline.BackgroundColor3 = Color3.new(0, 0, 0)
            outline.BorderSizePixel = 0
            outline.Size = UDim2.new(s.X.Scale, s.X.Offset + 2, s.Y.Scale, s.Y.Offset + 2)
            outline.Position = UDim2.new(p.X.Scale, p.X.Offset - 1, p.Y.Scale, p.Y.Offset - 1)
            outline.ZIndex = 1
            
            -- �곗깋 硫붿씤 �쇱씤
            local f = Instance.new("Frame", container)
            f.BackgroundColor3 = Color3.new(1, 1, 1)
            f.BorderSizePixel = 0
            f.Size = s
            f.Position = p
            f.ZIndex = 2
            
            return f, outline
        end
        
        -- 諛뺤뒪�� 4媛� �쇱씤 洹몃━湲�
        line(UDim2.new(1, 0, 0, 1), UDim2.new(0, 0, 0, 0))       -- �곷떒
        line(UDim2.new(1, 0, 0, 1), UDim2.new(0, 0, 1, -1))      -- �섎떒
        line(UDim2.new(0, 1, 1, 0), UDim2.new(0, 0, 0, 0))       -- 醫뚯륫
        line(UDim2.new(0, 1, 1, 0), UDim2.new(1, -1, 0, 0))      -- �곗륫
        
        local hGui = Instance.new("BillboardGui", root)
        hGui.Name = "HealthESP_Gui"
        hGui.Size = UDim2.new(1, 0, 5.5, 0)
        hGui.StudsOffset = Vector3.new(3, 0, 0) 
        hGui.AlwaysOnTop = true
        hGui.Enabled = HP_BAR_ENABLED and isEnemy(player)
        
        local barBg = Instance.new("Frame", hGui)
        barBg.Size = UDim2.new(0.2, 0, 1, 0)
        barBg.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
        barBg.BorderSizePixel = 0
        
        local hFill = Instance.new("Frame", barBg)
        hFill.Size = UDim2.new(1, 0, 1, 0)
        hFill.Position = UDim2.new(0, 0, 1, 0)
        hFill.AnchorPoint = Vector2.new(0, 1)
        hFill.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
        hFill.BorderSizePixel = 0
        
        local hText = Instance.new("TextLabel", barBg)
        hText.Size = UDim2.new(3, 0, 0.2, 0)
        hText.Position = UDim2.new(1.2, 0, 0, 0)
        hText.BackgroundTransparency = 1
        hText.TextColor3 = Color3.new(1, 1, 1)
        hText.TextScaled = true
        hText.Font = Enum.Font.SourceSansBold
        
        local function update()
            local p = math.clamp(hum.Health / hum.MaxHealth, 0, 1)
            hFill.Size = UDim2.new(1, 0, p, 0)
            hFill.BackgroundColor3 = Color3.fromHSV(p * 0.3, 1, 1)
            hText.Text = math.floor(hum.Health) .. "/" .. math.floor(hum.MaxHealth)
        end
        hum.HealthChanged:Connect(update)
        update()
    end
    player.CharacterAdded:Connect(onChar)
    if player.Character then onChar(player.Character) end
end

UIHandlers.UpdateSilentAim = CreateToggle("enabled (silent aim)", AimPage, function(v) SILENT_AIM_ENABLED = v end, nil, SILENT_AIM_ENABLED)
UIHandlers.UpdateAimLock = CreateToggle("Aim Lock", AimPage, function(v) 
    AIM_LOCK_ENABLED = v
    UpdateKeybindInfo()
    if KeybindInfoFrame.Visible and not MainFrame.Visible then
        HideKeybindInfo()
        task.wait(0.3)
        ShowKeybindInfo()
    end
end, "Keybind", AIM_LOCK_ENABLED)
UIHandlers.UpdateAutoAim = CreateToggle("Auto Aim", AimPage, function(v)
    AUTO_AIM_ENABLED = v
    if v then
        if AUTO_AIM_MODE == "Silent" then
            UIHandlers.UpdateSilentAim(true)
            UIHandlers.UpdateAimLock(false)
        else
            UIHandlers.UpdateSilentAim(false)
            UIHandlers.UpdateAimLock(true)
        end
    end
end, "AimMode", AUTO_AIM_ENABLED)

UIHandlers.UpdateAutoMatch = CreateToggle("Auto Match", AimPage, function(v)
    AUTO_MATCH_ENABLED = v
    if v then attemptJoinMatch() end
end, "MatchMode", AUTO_MATCH_ENABLED)

UIHandlers.UpdateFullAuto = CreateToggle("full Auto", AimPage, function(v)
    FULL_AUTO_ENABLED = v
    if v then
        local character = LocalPlayer.Character
        local humanoid = character and character:FindFirstChild("Humanoid")
        faLastHealth = humanoid and humanoid.Health or 100
        faIsFirstTeleport = true
        faTargetPlayer = nil
        faAutoClickPaused = false
        faIsRetreating = false
        faRetreatLock = false
        faLastRetreatTime = tick()
        faDisableGravity()
        faStartSpinning()
        faStartCameraLock()
        faStartAutoClick()
        if not faLoopActive then task.spawn(faMainLoop) end
        createNotification("Full Auto Started")
    else
        if faSpinConnection then faSpinConnection:Disconnect() faSpinConnection = nil end
        if faCameraConnection then faCameraConnection:Disconnect() faCameraConnection = nil end
        if faAutoClickConnection then faAutoClickConnection:Disconnect() faAutoClickConnection = nil end
        faEnableGravity()
        Camera.CameraType = Enum.CameraType.Custom
        faLoopActive = false
        createNotification("Full Auto Stopped")
    end
end, nil, FULL_AUTO_ENABLED)

UIHandlers.UpdateNoCoolTime = CreateToggle("No Cool Time (All)", AimPage, function(v)
    NO_COOL_TIME_ENABLED = v
    if v then
        removeCooldowns()
        noCoolTimeConnection = task.spawn(function()
            while NO_COOL_TIME_ENABLED do
                task.wait(10)
                if NO_COOL_TIME_ENABLED then pcall(removeCooldowns) end
            end
        end)
        if LocalPlayer.Character then hookCharacterForNoCoolTime() end
        createNotification("No Cool Time Enabled")
    else
        weaponDataCache = nil
        noCoolTimeApplied = false
        createNotification("No Cool Time Disabled")
    end
end, nil, NO_COOL_TIME_ENABLED)

UIHandlers.UpdateRageBot = CreateToggle("Rage bot", AimPage, function(v)
    RAGE_BOT_ENABLED = v
    UpdateKeybindInfo()
    if KeybindInfoFrame.Visible and not MainFrame.Visible then
        HideKeybindInfo()
        task.wait(0.3)
        ShowKeybindInfo()
    end
    if v then
        rbUpdateCharacterRefs()
        createNotification("Rage Bot Ready! Press [" .. formatKeyName(RAGE_BOT_KEY) .. "] to toggle")
    else
        rbEnabled = false
        if rbConnection then
            rbConnection:Disconnect()
            rbConnection = nil
        end
        if rbHrp then rbHrp.Velocity = Vector3.new(0, 0, 0) end
        createNotification("Rage Bot Disabled")
    end
end, "RageBotKeybind", RAGE_BOT_ENABLED)

CreateToggle("No spread,recoil", AimPage, function(v)
    NO_RECOIL_ENABLED = v
    if v then
        enableNoRecoil()
    else
        disableNoRecoil()
    end
end, nil, NO_RECOIL_ENABLED)

CreateToggle("enabled (esp)", VisualPage, function(v) ESP_ENABLED = v updateESPStates() end, nil, ESP_ENABLED)
CreateToggle("HP bar", VisualPage, function(v) HP_BAR_ENABLED = v updateESPStates() end, nil, HP_BAR_ENABLED)
CreateToggle("Notification", VisualPage, function(v) NOTIFICATION_ENABLED = v end, nil, NOTIFICATION_ENABLED)

CreateToggle("third person", VisualPage, function(v)
    THIRD_PERSON_ENABLED = v
    if v then
        enableThirdPerson()
    else
        disableThirdPerson()
    end
end, nil, THIRD_PERSON_ENABLED)

CreateToggle("Load skin changer", VisualPage, function(v)
    SKIN_CHANGER_ENABLED = v
    if v then
        local success, err = pcall(function()
            loadstring(game:HttpGet("https://[Log in to view URL]", true))()
        end)
        if success then
            createNotification("Skin Changer Loaded")
        else
            createNotification("Skin Changer Failed")
            warn("Skin Changer Error:", err)
        end
    else
        createNotification("Skin Changer Disabled")
    end
end, nil, SKIN_CHANGER_ENABLED)

CreateButton("mesh wrapping", VisualPage, function()
    local VM = workspace:FindFirstChild("ViewModels")
    local FP = VM and VM:FindFirstChild("FirstPerson")
    if FP then
        for _, o in ipairs(FP:GetDescendants()) do
            if o:IsA("MeshPart") then
                o.Material, o.Color = Enum.Material.ForceField, Color3.new(0, 0, 0)
                for _, f in ipairs({Enum.NormalId.Top, Enum.NormalId.Bottom, Enum.NormalId.Left, Enum.NormalId.Right, Enum.NormalId.Front, Enum.NormalId.Back}) do
                    local t = o:FindFirstChild("FixedTexture_" .. f.Name) or Instance.new("Texture", o)
                    t.Name = "FixedTexture_" .. f.Name
                    t.Texture = "rbxassetid://135989547473439"
                    t.Face = f
                    t.Color3 = Color3.new(0, 0, 0)
                    t.StudsPerTileU = 0.1
                    t.StudsPerTileV = 0.1
                end
            end
        end
    end
end)

local dragging, dragStart, startPos
MainFrame.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 and LOADING_COMPLETE and not HUB_ANIMATION_PLAYING then
        dragging = true
        dragStart = input.Position
        startPos = MainFrame.Position
    end
end)

UserInputService.InputChanged:Connect(function(input)
    if dragging and input.UserInputType == Enum.UserInputType.MouseMovement then
        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
end)

UserInputService.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        dragging = false
    end
end)

local function ToggleUI()
    if HUB_ANIMATION_PLAYING then return end
    if not LOADING_COMPLETE then return end
    if MainFrame.Visible then
        AnimateHubClose()
    else
        AnimateHubOpen()
    end
end

CloseBtn.MouseButton1Click:Connect(ToggleUI)
UserInputService.InputBegan:Connect(function(input, gpe)
    if not gpe and input.KeyCode == Enum.KeyCode.K and LOADING_COMPLETE then
        ToggleUI()
    end
end)

local function RunLoadingSequence()
    StartLoadingAnimation()
    task.wait(0.5)
    UpdateLoadingProgress(5, "Checking Core Services...")
    task.wait(0.2)
    UpdateLoadingProgress(15, "Core Services Ready")
    task.wait(0.15)
    UpdateLoadingProgress(20, "Waiting for Character...")
    local charReady = false
    for i = 1, 50 do
        local char = LocalPlayer.Character
        if char and char:FindFirstChild("HumanoidRootPart") and char:FindFirstChild("Humanoid") then
            charReady = true
            break
        end
        task.wait(0.05)
    end
    UpdateLoadingProgress(25, charReady and "Character Ready" or "Character Timeout")
    task.wait(0.15)
    UpdateLoadingProgress(30, "Initializing ESP System...")
    for _, p in pairs(Players:GetPlayers()) do
        pcall(function() createESP(p) end)
    end
    Players.PlayerAdded:Connect(createESP)
    UpdateLoadingProgress(40, "ESP System Ready")
    task.wait(0.15)
    UpdateLoadingProgress(45, "Setting up Player Tracking...")
    for _, p in ipairs(Players:GetPlayers()) do
        pcall(function() setupPlayerTracking(p) end)
    end
    Players.PlayerAdded:Connect(setupPlayerTracking)
    UpdateLoadingProgress(55, "Player Tracking Ready")
    task.wait(0.15)
    UpdateLoadingProgress(60, "Loading Weapon Modules...")
    if NO_COOL_TIME_ENABLED then
        pcall(function()
            lastCooldownRemoval = 0
            removeCooldowns()
            if LocalPlayer.Character then hookCharacterForNoCoolTime() end
            noCoolTimeConnection = task.spawn(function()
                while NO_COOL_TIME_ENABLED do
                    task.wait(10)
                    if NO_COOL_TIME_ENABLED then pcall(removeCooldowns) end
                end
            end)
        end)
    end
    if NO_RECOIL_ENABLED then
        pcall(function()
            initNoRecoilModules()
            enableNoRecoil()
        end)
    end
    UpdateLoadingProgress(70, "Weapon Modules Ready")
    task.wait(0.15)
    UpdateLoadingProgress(75, "Loading External Scripts...")
    if SKIN_CHANGER_ENABLED then
        local success = pcall(function()
            loadstring(game:HttpGet("https://***", true))()
        end)
        UpdateLoadingProgress(82, success and "Skin Changer Loaded" or "Skin Changer Failed")
    else
        UpdateLoadingProgress(82, "External Scripts Skipped")
    end
    task.wait(0.15)
    UpdateLoadingProgress(88, "Initializing Auto Features...")
    if FULL_AUTO_ENABLED then
        pcall(function()
            local character = LocalPlayer.Character
            local humanoid = character and character:FindFirstChild("Humanoid")
            faLastHealth = humanoid and humanoid.Health or 100
            faIsFirstTeleport = true
            faLastRetreatTime = tick()
            faDisableGravity()
            faStartSpinning()
            faStartCameraLock()
            faStartAutoClick()
            if not faLoopActive then task.spawn(faMainLoop) end
        end)
    end
    if AUTO_MATCH_ENABLED then pcall(attemptJoinMatch) end
    if RAGE_BOT_ENABLED then pcall(rbUpdateCharacterRefs) end
    if THIRD_PERSON_ENABLED then pcall(enableThirdPerson) end
    UpdateLoadingProgress(95, "Auto Features Ready")
    task.wait(0.15)
    UpdateLoadingProgress(98, "Applying Configuration...")
    updateESPStates()
    ShowTab("Aim")
    task.wait(0.1)
    UpdateLoadingProgress(100, "All Systems Loaded!")
    task.wait(0.2)
    EndLoadingAnimation(function()
        task.wait(0.5)
        ShowKeybindInfo()
    end)
end

if FULL_AUTO_ENABLED then
    LOADING_COMPLETE = true
    MainFrame.Visible = false
    LoadingOverlay.Visible = false
    ContentContainer.Visible = true
    for _, p in pairs(Players:GetPlayers()) do
        pcall(function() createESP(p) end)
        pcall(function() setupPlayerTracking(p) end)
    end
    Players.PlayerAdded:Connect(createESP)
    Players.PlayerAdded:Connect(setupPlayerTracking)
    local character = LocalPlayer.Character
    local humanoid = character and character:FindFirstChild("Humanoid")
    faLastHealth = humanoid and humanoid.Health or 100
    faIsFirstTeleport = true
    faTargetPlayer = nil
    faAutoClickPaused = false
    faIsRetreating = false
    faRetreatLock = false
    faLastRetreatTime = tick()
    faDisableGravity()
    faStartSpinning()
    faStartCameraLock()
    faStartAutoClick()
    if not faLoopActive then task.spawn(faMainLoop) end
    if AUTO_MATCH_ENABLED then attemptJoinMatch() end
    if SKIN_CHANGER_ENABLED then
        pcall(function()
            loadstring(game:HttpGet("https://[Log in to view URL]", true))()
        end)
    end
    if NO_COOL_TIME_ENABLED then
        pcall(function()
            lastCooldownRemoval = 0
            removeCooldowns()
            if LocalPlayer.Character then hookCharacterForNoCoolTime() end
            noCoolTimeConnection = task.spawn(function()
                while NO_COOL_TIME_ENABLED do
                    task.wait(10)
                    if NO_COOL_TIME_ENABLED then pcall(removeCooldowns) end
                end
            end)
        end)
    end
    if NO_RECOIL_ENABLED then
        pcall(function()
            initNoRecoilModules()
            enableNoRecoil()
        end)
    end
    if RAGE_BOT_ENABLED then rbUpdateCharacterRefs() end
    if THIRD_PERSON_ENABLED then enableThirdPerson() end
    updateESPStates()
    ShowTab("Aim")
    task.wait(1)
    ShowKeybindInfo()
else
    task.spawn(RunLoadingSequence)
end

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: