Загрузка данных


-- Grechka Hub - Persistent Functions + 3D Box ESP
-- Все функции сохраняются после респавна

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

-- Settings (сохраняются при респавне)
local GrechkaHub = {
    ESP = false,
    BoxESP = false,
    Chams = false,
    Fly = false,
    SpeedHack = false,
    InfiniteJump = false,
    NoClip = false,
    ESPObjects = {},
    Connections = {}
}

-- GUI
local ScreenGui = Instance.new("ScreenGui")
ScreenGui.Name = "GrechkaHub"
ScreenGui.Parent = game.CoreGui
ScreenGui.ResetOnSpawn = false
ScreenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling

local MainFrame = Instance.new("Frame")
MainFrame.Size = UDim2.new(0, 500, 0, 400)
MainFrame.Position = UDim2.new(0.5, -250, 0.5, -200)
MainFrame.BackgroundColor3 = Color3.fromRGB(20, 20, 20)
MainFrame.BorderSizePixel = 0
MainFrame.Active = true
MainFrame.Draggable = true
MainFrame.Parent = ScreenGui

local UICorner = Instance.new("UICorner")
UICorner.CornerRadius = UDim.new(0, 8)
UICorner.Parent = MainFrame

-- Title Bar
local TitleBar = Instance.new("Frame")
TitleBar.Size = UDim2.new(1, 0, 0, 35)
TitleBar.BackgroundColor3 = Color3.fromRGB(15, 15, 15)
TitleBar.BorderSizePixel = 0
TitleBar.Parent = MainFrame

local Title = Instance.new("TextLabel")
Title.Size = UDim2.new(1, -10, 1, 0)
Title.Position = UDim2.new(0, 10, 0, 0)
Title.BackgroundTransparency = 1
Title.TextColor3 = Color3.fromRGB(255, 200, 0)
Title.Text = "GRECHKA HUB"
Title.Font = Enum.Font.GothamBold
Title.TextSize = 18
Title.TextXAlignment = Enum.TextXAlignment.Left
Title.Parent = TitleBar

-- Tab Buttons Frame
local TabFrame = Instance.new("Frame")
TabFrame.Size = UDim2.new(0, 100, 1, -35)
TabFrame.Position = UDim2.new(0, 0, 0, 35)
TabFrame.BackgroundColor3 = Color3.fromRGB(15, 15, 15)
TabFrame.BorderSizePixel = 0
TabFrame.Parent = MainFrame

-- Content Frame
local ContentFrame = Instance.new("Frame")
ContentFrame.Size = UDim2.new(1, -100, 1, -35)
ContentFrame.Position = UDim2.new(0, 100, 0, 35)
ContentFrame.BackgroundColor3 = Color3.fromRGB(25, 25, 25)
ContentFrame.BorderSizePixel = 0
ContentFrame.Parent = MainFrame

-- Pages
local Pages = {}

local function CreatePage(name)
    local Page = Instance.new("ScrollingFrame")
    Page.Size = UDim2.new(1, -20, 1, -20)
    Page.Position = UDim2.new(0, 10, 0, 10)
    Page.BackgroundTransparency = 1
    Page.BorderSizePixel = 0
    Page.ScrollBarThickness = 4
    Page.ScrollBarImageColor3 = Color3.fromRGB(255, 200, 0)
    Page.Visible = false
    Page.CanvasSize = UDim2.new(0, 0, 0, 0)
    Page.Parent = ContentFrame
    
    local UIListLayout = Instance.new("UIListLayout")
    UIListLayout.Padding = UDim.new(0, 8)
    UIListLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
    UIListLayout.SortOrder = Enum.SortOrder.LayoutOrder
    UIListLayout.Parent = Page
    
    Pages[name] = Page
    return Page
end

local function ShowPage(name)
    for _, page in pairs(Pages) do
        page.Visible = false
    end
    if Pages[name] then
        Pages[name].Visible = true
    end
end

-- Create Toggle
local function CreateToggle(page, text, default, callback)
    local ToggleFrame = Instance.new("Frame")
    ToggleFrame.Size = UDim2.new(1, -20, 0, 45)
    ToggleFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
    ToggleFrame.BorderSizePixel = 0
    ToggleFrame.Parent = page
    
    local ToggleCorner = Instance.new("UICorner")
    ToggleCorner.CornerRadius = UDim.new(0, 6)
    ToggleCorner.Parent = ToggleFrame
    
    local ToggleLabel = Instance.new("TextLabel")
    ToggleLabel.Size = UDim2.new(0, 200, 1, 0)
    ToggleLabel.Position = UDim2.new(0, 10, 0, 0)
    ToggleLabel.BackgroundTransparency = 1
    ToggleLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
    ToggleLabel.Text = text
    ToggleLabel.Font = Enum.Font.Gotham
    ToggleLabel.TextSize = 14
    ToggleLabel.TextXAlignment = Enum.TextXAlignment.Left
    ToggleLabel.Parent = ToggleFrame
    
    local ToggleButton = Instance.new("TextButton")
    ToggleButton.Size = UDim2.new(0, 50, 0, 24)
    ToggleButton.Position = UDim2.new(1, -65, 0.5, -12)
    ToggleButton.BackgroundColor3 = default and Color3.fromRGB(255, 200, 0) or Color3.fromRGB(60, 60, 60)
    ToggleButton.Text = ""
    ToggleButton.BorderSizePixel = 0
    ToggleButton.AutoButtonColor = false
    ToggleButton.Parent = ToggleFrame
    
    local ToggleButtonCorner = Instance.new("UICorner")
    ToggleButtonCorner.CornerRadius = UDim.new(0, 12)
    ToggleButtonCorner.Parent = ToggleButton
    
    local ToggleIndicator = Instance.new("Frame")
    ToggleIndicator.Size = UDim2.new(0, 18, 0, 18)
    ToggleIndicator.Position = default and UDim2.new(1, -21, 0.5, -9) or UDim2.new(0, 3, 0.5, -9)
    ToggleIndicator.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    ToggleIndicator.BorderSizePixel = 0
    ToggleIndicator.Parent = ToggleButton
    
    local IndicatorCorner = Instance.new("UICorner")
    IndicatorCorner.CornerRadius = UDim.new(1, 0)
    IndicatorCorner.Parent = ToggleIndicator
    
    local enabled = default
    callback(enabled) -- Вызываем колбэк сразу для применения состояния по умолчанию
    
    local function Toggle()
        enabled = not enabled
        local goal = enabled and UDim2.new(1, -21, 0.5, -9) or UDim2.new(0, 3, 0.5, -9)
        
        TweenService:Create(ToggleIndicator, TweenInfo.new(0.2), {Position = goal}):Play()
        TweenService:Create(ToggleButton, TweenInfo.new(0.2), {
            BackgroundColor3 = enabled and Color3.fromRGB(255, 200, 0) or Color3.fromRGB(60, 60, 60)
        }):Play()
        
        callback(enabled)
    end
    
    ToggleButton.MouseButton1Click:Connect(Toggle)
    
    page.CanvasSize = UDim2.new(0, 0, 0, page.UIListLayout.AbsoluteContentSize.Y + 10)
    
    return {Toggle = Toggle, GetState = function() return enabled end}
end

local function CreateButton(page, text, callback)
    local Button = Instance.new("TextButton")
    Button.Size = UDim2.new(1, -20, 0, 35)
    Button.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
    Button.TextColor3 = Color3.fromRGB(255, 255, 255)
    Button.Text = text
    Button.Font = Enum.Font.Gotham
    Button.TextSize = 14
    Button.BorderSizePixel = 0
    Button.AutoButtonColor = false
    Button.Parent = page
    
    local ButtonCorner = Instance.new("UICorner")
    ButtonCorner.CornerRadius = UDim.new(0, 6)
    ButtonCorner.Parent = Button
    
    Button.MouseButton1Click:Connect(callback)
    
    Button.MouseEnter:Connect(function()
        TweenService:Create(Button, TweenInfo.new(0.2), {BackgroundColor3 = Color3.fromRGB(255, 200, 0)}):Play()
        TweenService:Create(Button, TweenInfo.new(0.2), {TextColor3 = Color3.fromRGB(0, 0, 0)}):Play()
    end)
    
    Button.MouseLeave:Connect(function()
        TweenService:Create(Button, TweenInfo.new(0.2), {BackgroundColor3 = Color3.fromRGB(30, 30, 30)}):Play()
        TweenService:Create(Button, TweenInfo.new(0.2), {TextColor3 = Color3.fromRGB(255, 255, 255)}):Play()
    end)
    
    page.CanvasSize = UDim2.new(0, 0, 0, page.UIListLayout.AbsoluteContentSize.Y + 10)
    return Button
end

-- Pages Creation
local VisualPage = CreatePage("Visual")
local PlayerPage = CreatePage("Player")
local SettingsPage = CreatePage("Settings")

-- ================== ESP SYSTEM (3D BOX) ==================
local ESPFolder = Instance.new("Folder")
ESPFolder.Name = "GrechkaESP"
ESPFolder.Parent = game.CoreGui

-- Функция для получения всех 8 углов 3D Box персонажа
local function GetCharacterCorners(character)
    local head = character:FindFirstChild("Head")
    local hrp = character:FindFirstChild("HumanoidRootPart")
    
    if not head or not hrp then return nil end
    
    -- Определяем размеры персонажа
    local headSize = head.Size
    local characterWidth = 2  -- ширина персонажа
    local characterDepth = 1  -- глубина персонажа
    
    -- Находим низ (используем ноги если есть)
    local lowestY = hrp.Position.Y - 3
    local leftFoot = character:FindFirstChild("LeftFoot") or character:FindFirstChild("Left Leg")
    local rightFoot = character:FindFirstChild("RightFoot") or character:FindFirstChild("Right Leg")
    
    if leftFoot and rightFoot then
        lowestY = math.min(leftFoot.Position.Y, rightFoot.Position.Y)
    elseif leftFoot then
        lowestY = leftFoot.Position.Y
    elseif rightFoot then
        lowestY = rightFoot.Position.Y
    end
    
    -- Верхняя точка
    local highestY = head.Position.Y + headSize.Y / 2
    
    -- Центральная позиция
    local cx = hrp.Position.X
    local cy = lowestY
    local cz = hrp.Position.Z
    
    -- Половины размеров
    local hw = characterWidth / 2
    local hh = highestY - lowestY
    local hd = characterDepth / 2
    
    -- 8 углов 3D Box
    local corners = {
        -- Нижняя плоскость
        Vector3.new(cx - hw, cy, cz - hd),  -- 1: низ-лево-близко
        Vector3.new(cx + hw, cy, cz - hd),  -- 2: низ-право-близко
        Vector3.new(cx + hw, cy, cz + hd),  -- 3: низ-право-далеко
        Vector3.new(cx - hw, cy, cz + hd),  -- 4: низ-лево-далеко
        
        -- Верхняя плоскость
        Vector3.new(cx - hw, cy + hh, cz - hd),  -- 5: верх-лево-близко
        Vector3.new(cx + hw, cy + hh, cz - hd),  -- 6: верх-право-близко
        Vector3.new(cx + hw, cy + hh, cz + hd),  -- 7: верх-право-далеко
        Vector3.new(cx - hw, cy + hh, cz + hd),  -- 8: верх-лево-далеко
    }
    
    return corners
end

-- Функция для получения центра персонажа (для имени)
local function GetCharacterCenter(character)
    local head = character:FindFirstChild("Head")
    if not head then return nil end
    
    local headSize = head.Size
    return head.Position + Vector3.new(0, headSize.Y / 2 + 0.5, 0)
end

local function CreateESP(player)
    local drawings = {}
    
    -- Создаём 12 линий для 3D Box (все рёбра куба)
    local edges = {
        -- Нижняя плоскость
        {1, 2}, {2, 3}, {3, 4}, {4, 1},
        -- Верхняя плоскость
        {5, 6}, {6, 7}, {7, 8}, {8, 5},
        -- Вертикальные рёбра
        {1, 5}, {2, 6}, {3, 7}, {4, 8}
    }
    
    local boxLines = {}
    for i = 1, 12 do
        local line = Drawing.new("Line")
        line.Thickness = 2
        line.Color = Color3.fromRGB(255, 255, 255)
        line.Visible = false
        line.ZIndex = 2
        table.insert(boxLines, line)
    end
    
    -- Имя над головой
    local nameTag = Drawing.new("Text")
    nameTag.Text = player.Name
    nameTag.Size = 14
    nameTag.Color = Color3.fromRGB(255, 255, 255)
    nameTag.Center = true
    nameTag.Outline = true
    nameTag.OutlineColor = Color3.fromRGB(0, 0, 0)
    nameTag.Visible = false
    nameTag.ZIndex = 3
    
    -- Расстояние
    local distanceTag = Drawing.new("Text")
    distanceTag.Text = ""
    distanceTag.Size = 13
    distanceTag.Color = Color3.fromRGB(200, 200, 200)
    distanceTag.Center = true
    distanceTag.Outline = true
    distanceTag.OutlineColor = Color3.fromRGB(0, 0, 0)
    distanceTag.Visible = false
    distanceTag.ZIndex = 3
    
    -- Оружие
    local weaponTag = Drawing.new("Text")
    weaponTag.Text = ""
    weaponTag.Size = 12
    weaponTag.Color = Color3.fromRGB(255, 200, 0)
    weaponTag.Center = true
    weaponTag.Outline = true
    weaponTag.OutlineColor = Color3.fromRGB(0, 0, 0)
    weaponTag.Visible = false
    weaponTag.ZIndex = 3
    
    -- Health Bar
    local healthBar = Drawing.new("Square")
    healthBar.Thickness = 1
    healthBar.Color = Color3.fromRGB(0, 255, 0)
    healthBar.Filled = true
    healthBar.Visible = false
    healthBar.ZIndex = 2
    
    local healthBg = Drawing.new("Square")
    healthBg.Thickness = 1
    healthBg.Color = Color3.fromRGB(30, 30, 30)
    healthBg.Filled = true
    healthBg.Visible = false
    healthBg.ZIndex = 1
    
    local espContainer = Instance.new("Folder")
    espContainer.Name = player.Name
    espContainer.Parent = ESPFolder
    
    local espData = {
        BoxLines = boxLines,
        NameTag = nameTag,
        Distance = distanceTag,
        Weapon = weaponTag,
        HealthBar = healthBar,
        HealthBg = healthBg,
        Container = espContainer,
        Player = player
    }
    
    GrechkaHub.ESPObjects[player] = espData
    
    -- Функция обновления ESP для конкретного игрока
    local function updateESP()
        local character = player.Character
        if not character then
            -- Скрываем всё если персонажа нет
            for _, line in ipairs(boxLines) do line.Visible = false end
            nameTag.Visible = false
            distanceTag.Visible = false
            weaponTag.Visible = false
            healthBar.Visible = false
            healthBg.Visible = false
            return
        end
        
        local head = character:FindFirstChild("Head")
        local hrp = character:FindFirstChild("HumanoidRootPart")
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        
        if not head or not hrp or not humanoid then
            for _, line in ipairs(boxLines) do line.Visible = false end
            nameTag.Visible = false
            distanceTag.Visible = false
            weaponTag.Visible = false
            healthBar.Visible = false
            healthBg.Visible = false
            return
        end
        
        local health = humanoid.Health
        local maxHealth = humanoid.MaxHealth
        
        -- 3D BOX
        local corners = GetCharacterCorners(character)
        if corners and GrechkaHub.BoxESP then
            -- Конвертируем все 8 углов в экранные координаты
            local screenCorners = {}
            for i, corner in ipairs(corners) do
                local screenPos, onScreen = Camera:WorldToViewportPoint(corner)
                screenCorners[i] = {pos = Vector2.new(screenPos.X, screenPos.Y), onScreen = onScreen}
            end
            
            -- Рисуем 12 рёбер
            for i, edge in ipairs(edges) do
                local c1 = screenCorners[edge[1]]
                local c2 = screenCorners[edge[2]]
                
                if c1.onScreen and c2.onScreen then
                    boxLines[i].From = c1.pos
                    boxLines[i].To = c2.pos
                    boxLines[i].Visible = health > 0
                else
                    boxLines[i].Visible = false
                end
            end
        else
            for _, line in ipairs(boxLines) do line.Visible = false end
        end
        
        -- ИМЯ, РАССТОЯНИЕ, ОРУЖИЕ
        if GrechkaHub.ESP then
            local centerPoint = GetCharacterCenter(character)
            if centerPoint then
                local screenPos, onScreen = Camera:WorldToViewportPoint(centerPoint)
                
                if onScreen then
                    local distance = (LocalPlayer.Character and LocalPlayer.Character:FindFirstChild("HumanoidRootPart"))
                        and (LocalPlayer.Character.HumanoidRootPart.Position - hrp.Position).Magnitude or 0
                    
                    local dynamicSize = math.clamp(400 / math.max(distance, 1), 10, 18)
                    
                    -- Имя
                    nameTag.Position = Vector2.new(screenPos.X, screenPos.Y)
                    nameTag.Size = dynamicSize
                    nameTag.Visible = health > 0
                    
                    -- Расстояние
                    distanceTag.Text = string.format("%.0f m", distance)
                    distanceTag.Position = Vector2.new(screenPos.X, screenPos.Y + dynamicSize + 2)
                    distanceTag.Size = dynamicSize - 2
                    distanceTag.Visible = health > 0
                    
                    -- Оружие
                    local tool = nil
                    for _, v in pairs(character:GetChildren()) do
                        if v:IsA("Tool") then
                            tool = v
                            break
                        end
                    end
                    if tool then
                        weaponTag.Text = "[" .. tool.Name .. "]"
                        weaponTag.Position = Vector2.new(screenPos.X, screenPos.Y + dynamicSize * 2 + 4)
                        weaponTag.Size = dynamicSize - 2
                        weaponTag.Visible = health > 0
                    else
                        weaponTag.Visible = false
                    end
                else
                    nameTag.Visible = false
                    distanceTag.Visible = false
                    weaponTag.Visible = false
                end
            end
        else
            nameTag.Visible = false
            distanceTag.Visible = false
            weaponTag.Visible = false
        end
        
        -- HEALTH BAR (слева от бокса)
        if GrechkaHub.ESP and GrechkaHub.BoxESP and corners then
            local topScreen = Camera:WorldToViewportPoint(corners[5])
            local bottomScreen = Camera:WorldToViewportPoint(corners[1])
            local leftScreen = Camera:WorldToViewportPoint(corners[1])
            
            if topScreen and bottomScreen then
                local barWidth = 3
                local barHeight = math.abs(bottomScreen.Y - topScreen.Y)
                local barX = leftScreen.X - barWidth - 6
                local barY = topScreen.Y
                
                healthBg.Position = Vector2.new(barX, barY)
                healthBg.Size = Vector2.new(barWidth, barHeight)
                healthBg.Visible = health > 0
                
                local healthPercent = health / maxHealth
                healthBar.Position = Vector2.new(barX, barY + barHeight * (1 - healthPercent))
                healthBar.Size = Vector2.new(barWidth, barHeight * healthPercent)
                healthBar.Visible = health > 0
                
                if healthPercent > 0.5 then
                    healthBar.Color = Color3.fromRGB(0, 255, 0)
                elseif healthPercent > 0.25 then
                    healthBar.Color = Color3.fromRGB(255, 255, 0)
                else
                    healthBar.Color = Color3.fromRGB(255, 0, 0)
                end
            end
        else
            healthBar.Visible = false
            healthBg.Visible = false
        end
    end
    
    -- Запускаем обновление
    local connection = RunService.RenderStepped:Connect(updateESP)
    espData.Connection = connection
    
    -- Отслеживаем респавн персонажа
    local function onCharacterAdded(character)
        -- Ничего не делаем, просто ждём когда updateESP подхватит новый character
    end
    
    if player.Character then
        onCharacterAdded(player.Character)
    end
    
    player.CharacterAdded:Connect(onCharacterAdded)
    espData.CharacterAddedConn = player.CharacterAdded:Connect(onCharacterAdded)
end

local function RemoveESP(player)
    if GrechkaHub.ESPObjects[player] then
        local obj = GrechkaHub.ESPObjects[player]
        if obj.Connection then obj.Connection:Disconnect() end
        if obj.CharacterAddedConn then obj.CharacterAddedConn:Disconnect() end
        for _, line in ipairs(obj.BoxLines) do
            if line then line:Remove() end
        end
        if obj.NameTag then obj.NameTag:Remove() end
        if obj.Distance then obj.Distance:Remove() end
        if obj.Weapon then obj.Weapon:Remove() end
        if obj.HealthBar then obj.HealthBar:Remove() end
        if obj.HealthBg then obj.HealthBg:Remove() end
        if obj.Container then obj.Container:Destroy() end
        GrechkaHub.ESPObjects[player] = nil
    end
end

-- Создаём ESP для всех текущих игроков
for _, player in pairs(Players:GetPlayers()) do
    if player ~= LocalPlayer then
        CreateESP(player)
    end
end

-- Отслеживаем новых игроков
Players.PlayerAdded:Connect(function(player)
    if player ~= LocalPlayer then
        CreateESP(player)
    end
end)

Players.PlayerRemoving:Connect(function(player)
    RemoveESP(player)
end)

-- ================== FUNCTIONS (СОХРАНЯЮТСЯ ПОСЛЕ РЕСПАВНА) ==================

-- Fly Function
local function EnableFly()
    if GrechkaHub.FlyConnection then
        GrechkaHub.FlyConnection:Disconnect()
        GrechkaHub.FlyConnection = nil
    end
    
    local character = LocalPlayer.Character
    if not character then return end
    
    local rootPart = character:FindFirstChild("HumanoidRootPart")
    local humanoid = character:FindFirstChild("Humanoid")
    if not rootPart or not humanoid then return end
    
    humanoid.PlatformStand = true
    
    local bodyGyro = Instance.new("BodyGyro")
    bodyGyro.MaxTorque = Vector3.new(400000, 400000, 400000)
    bodyGyro.P = 12500
    bodyGyro.Parent = rootPart
    
    local bodyVelocity = Instance.new("BodyVelocity")
    bodyVelocity.MaxForce = Vector3.new(400000, 400000, 400000)
    bodyVelocity.Velocity = Vector3.new(0, 0, 0)
    bodyVelocity.Parent = rootPart
    
    GrechkaHub.FlyConnection = RunService.Heartbeat:Connect(function()
        if not GrechkaHub.Fly then return end
        local speed = 50
        bodyGyro.CFrame = Camera.CFrame
        
        if UserInputService:IsKeyDown(Enum.KeyCode.W) then
            bodyVelocity.Velocity = Camera.CFrame.LookVector * speed
        elseif UserInputService:IsKeyDown(Enum.KeyCode.S) then
            bodyVelocity.Velocity = -Camera.CFrame.LookVector * speed
        elseif UserInputService:IsKeyDown(Enum.KeyCode.A) then
            bodyVelocity.Velocity = -Camera.CFrame.RightVector * speed
        elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
            bodyVelocity.Velocity = Camera.CFrame.RightVector * speed
        elseif UserInputService:IsKeyDown(Enum.KeyCode.Space) then
            bodyVelocity.Velocity = Vector3.new(0, speed, 0)
        elseif UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
            bodyVelocity.Velocity = Vector3.new(0, -speed, 0)
        else
            bodyVelocity.Velocity = Vector3.new(0, 0, 0)
        end
    end)
end

local function DisableFly()
    if GrechkaHub.FlyConnection then
        GrechkaHub.FlyConnection:Disconnect()
        GrechkaHub.FlyConnection = nil
    end
    
    local character = LocalPlayer.Character
    if character then
        local rootPart = character:FindFirstChild("HumanoidRootPart")
        local humanoid = character:FindFirstChild("Humanoid")
        if rootPart then
            if rootPart:FindFirstChild("BodyGyro") then rootPart.BodyGyro:Destroy() end
            if rootPart:FindFirstChild("BodyVelocity") then rootPart.BodyVelocity:Destroy() end
        end
        if humanoid then
            humanoid.PlatformStand = false
        end
    end
end

-- Speed Hack
local function ApplySpeed()
    local character = LocalPlayer.Character
    if character then
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
            humanoid.WalkSpeed = GrechkaHub.SpeedHack and 100 or 16
        end
    end
end

-- Infinite Jump
local function SetupInfiniteJump()
    if GrechkaHub.JumpConnection then
        GrechkaHub.JumpConnection:Disconnect()
        GrechkaHub.JumpConnection = nil
    end
    
    if GrechkaHub.InfiniteJump then
        GrechkaHub.JumpConnection = UserInputService.JumpRequest:Connect(function()
            if not GrechkaHub.InfiniteJump then return end
            local character = LocalPlayer.Character
            if character then
                local humanoid = character:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
                end
            end
        end)
    end
end

-- NoClip
local function SetupNoClip()
    if GrechkaHub.NoClipConnection then
        GrechkaHub.NoClipConnection:Disconnect()
        GrechkaHub.NoClipConnection = nil
    end
    
    if GrechkaHub.NoClip then
        GrechkaHub.NoClipConnection = RunService.Stepped:Connect(function()
            if not GrechkaHub.NoClip then return end
            if LocalPlayer.Character then
                for _, part in pairs(LocalPlayer.Character:GetDescendants()) do
                    if part:IsA("BasePart") then
                        part.CanCollide = false
                    end
                end
            end
        end)
    end
end

-- Chams
local function ApplyChams(state)
    for _, player in pairs(Players:GetPlayers()) do
        if player ~= LocalPlayer and player.Character then
            for _, part in pairs(player.Character:GetChildren()) do
                if part:IsA("BasePart") then
                    if state then
                        part.Material = Enum.Material.ForceField
                        part.Color = Color3.fromRGB(255, 0, 0)
                    else
                        part.Material = Enum.Material.Plastic
                    end
                end
            end
        end
    end
end

-- Отслеживаем респавн локального игрока для повторного применения функций
LocalPlayer.CharacterAdded:Connect(function(character)
    -- Ждём загрузки персонажа
    character:WaitForChild("Humanoid")
    character:WaitForChild("HumanoidRootPart")
    
    wait(0.5) -- Небольшая задержка для полной загрузки
    
    -- Повторно применяем все активные функции
    if GrechkaHub.Fly then
        EnableFly()
    end
    
    if GrechkaHub.SpeedHack then
        ApplySpeed()
    end
    
    SetupInfiniteJump()
    SetupNoClip()
    
    if GrechkaHub.Chams then
        ApplyChams(true)
    end
end)

-- Отслеживаем новых персонажей других игроков для Chams
Players.PlayerAdded:Connect(function(player)
    if player ~= LocalPlayer then
        player.CharacterAdded:Connect(function(character)
            if GrechkaHub.Chams then
                wait(0.5)
                for _, part in pairs(character:GetChildren()) do
                    if part:IsA("BasePart") then
                        part.Material = Enum.Material.ForceField
                        part.Color = Color3.fromRGB(255, 0, 0)
                    end
                end
            end
        end)
    end
end)

-- ================== TOGGLES ==================

-- Visual Page
CreateToggle(VisualPage, "ESP", false, function(state)
    GrechkaHub.ESP = state
end)

CreateToggle(VisualPage, "3D Box ESP", false, function(state)
    GrechkaHub.BoxESP = state
end)

CreateToggle(VisualPage, "Chams", false, function(state)
    GrechkaHub.Chams = state
    ApplyChams(state)
end)

-- ESP Colors
CreateButton(VisualPage, "ESP Color: White", function()
    for _, obj in pairs(GrechkaHub.ESPObjects) do
        if obj.NameTag then obj.NameTag.Color = Color3.fromRGB(255, 255, 255) end
        for _, line in ipairs(obj.BoxLines) do
            if line then line.Color = Color3.fromRGB(255, 255, 255) end
        end
    end
end)

CreateButton(VisualPage, "ESP Color: Red", function()
    for _, obj in pairs(GrechkaHub.ESPObjects) do
        if obj.NameTag then obj.NameTag.Color = Color3.fromRGB(255, 0, 0) end
        for _, line in ipairs(obj.BoxLines) do
            if line then line.Color = Color3.fromRGB(255, 0, 0) end
        end
    end
end)

CreateButton(VisualPage, "ESP Color: Green", function()
    for _, obj in pairs(GrechkaHub.ESPObjects) do
        if obj.NameTag then obj.NameTag.Color = Color3.fromRGB(0, 255, 0) end
        for _, line in ipairs(obj.BoxLines) do
            if line then line.Color = Color3.fromRGB(0, 255, 0) end
        end
    end
end)

CreateButton(VisualPage, "ESP Color: Yellow", function()
    for _, obj in pairs(GrechkaHub.ESPObjects) do
        if obj.NameTag then obj.NameTag.Color = Color3.fromRGB(255, 200, 0) end
        for _, line in ipairs(obj.BoxLines) do
            if line then line.Color = Color3.fromRGB(255, 200, 0) end
        end
    end
end)

CreateButton(VisualPage, "ESP Color: Blue", function()
    for _, obj in pairs(GrechkaHub.ESPObjects) do
        if obj.NameTag then obj.NameTag.Color = Color3.fromRGB(0, 150, 255) end
        for _, line in ipairs(obj.BoxLines) do
            if line then line.Color = Color3.fromRGB(0, 150, 255) end
        end
    end
end)

-- Player Page
CreateToggle(PlayerPage, "Fly", false, function(state)
    GrechkaHub.Fly = state
    if state then
        EnableFly()
    else
        DisableFly()
    end
end)

CreateToggle(PlayerPage, "Speed Hack", false, function(state)
    GrechkaHub.SpeedHack = state
    ApplySpeed()
end)

CreateToggle(PlayerPage, "Infinite Jump", false, function(state)
    GrechkaHub.InfiniteJump = state
    SetupInfiniteJump()
end)

CreateToggle(PlayerPage, "No Clip", false, function(state)
    GrechkaHub.NoClip = state
    SetupNoClip()
end)

-- Settings Page
CreateButton(SettingsPage, "Unlock FPS", function()
    setfpscap(999)
end)

CreateButton(SettingsPage, "Rejoin Server", function()
    local ts = game:GetService("TeleportService")
    ts:Teleport(game.PlaceId, LocalPlayer)
end)

CreateButton(SettingsPage, "Server Hop", function()
    local Http = game:GetService("HttpService")
    local ts = game:GetService("TeleportService")
    local servers = Http:JSONDecode(game:HttpGet("https://games.roblox.com/v1/games/" .. game.PlaceId .. "/servers/Public?limit=100"))
    if #servers.data > 0 then
        ts:TeleportToPlaceInstance(game.PlaceId, servers.data[math.random(1, #servers.data)].id, LocalPlayer)
    end
end)

-- Tab Buttons
local Tabs = {
    {Name = "Visual", Page = "Visual"},
    {Name = "Player", Page = "Player"},
    {Name = "Settings", Page = "Settings"},
}

local TabButtons = {}
for i, tab in ipairs(Tabs) do
    local TabButton = Instance.new("TextButton")
    TabButton.Size = UDim2.new(1, -10, 0, 35)
    TabButton.Position = UDim2.new(0, 5, 0, 5 + (i - 1) * 40)
    TabButton.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
    TabButton.TextColor3 = Color3.fromRGB(200, 200, 200)
    TabButton.Text = tab.Name
    TabButton.Font = Enum.Font.Gotham
    TabButton.TextSize = 14
    TabButton.BorderSizePixel = 0
    TabButton.AutoButtonColor = false
    TabButton.Parent = TabFrame
    
    local TabCorner2 = Instance.new("UICorner")
    TabCorner2.CornerRadius = UDim.new(0, 6)
    TabCorner2.Parent = TabButton
    
    TabButton.MouseButton1Click:Connect(function()
        ShowPage(tab.Page)
        for _, btn in ipairs(TabButtons) do
            btn.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
            btn.TextColor3 = Color3.fromRGB(200, 200, 200)
        end
        TabButton.BackgroundColor3 = Color3.fromRGB(255, 200, 0)
        TabButton.TextColor3 = Color3.fromRGB(0, 0, 0)
    end)
    
    table.insert(TabButtons, TabButton)
end

-- Default page
ShowPage("Visual")
TabButtons[1].BackgroundColor3 = Color3.fromRGB(255, 200, 0)
TabButtons[1].TextColor3 = Color3.fromRGB(0, 0, 0)

-- Close button
local CloseButton = Instance.new("TextButton")
CloseButton.Size = UDim2.new(0, 25, 0, 25)
CloseButton.Position = UDim2.new(1, -30, 0, 5)
CloseButton.BackgroundColor3 = Color3.fromRGB(255, 50, 50)
CloseButton.TextColor3 = Color3.fromRGB(255, 255, 255)
CloseButton.Text = "X"
CloseButton.Font = Enum.Font.GothamBold
CloseButton.TextSize = 14
CloseButton.BorderSizePixel = 0
CloseButton.AutoButtonColor = false
CloseButton.Parent = TitleBar

local CloseCorner = Instance.new("UICorner")
CloseCorner.CornerRadius = UDim.new(0, 12)
CloseCorner.Parent = CloseButton

CloseButton.MouseButton1Click:Connect(function()
    for player, _ in pairs(GrechkaHub.ESPObjects) do
        RemoveESP(player)
    end
    if ESPFolder then ESPFolder:Destroy() end
    ScreenGui:Destroy()
end)

-- Toggle GUI
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.RightShift then
        MainFrame.Visible = not MainFrame.Visible
    end
end)

ScreenGui.ResetOnSpawn = false

print("Grechka Hub загружен!")
print("RightShift - скрыть/показать меню")
print("Все функции сохраняются после респавна!")
print("3D Box ESP от ног до головы!")