This is a well-structured Roblox client-side script for a game called "Cut BrainRot Simulator." It handles the user interface, player interactions, and communication with the server via RemoteEvents. Below, I'll provide an analysis of the script, highlight its strengths, identify potential improvements, and suggest additional features or optimizations. I'll also address any implicit questions you might have, such as how to enhance or debug this script.

Analysis of the Script
Purpose
The script is a client-side script for a Roblox simulator game where players "cut" BrainRot objects to earn currency, upgrade weapons, unlock areas, purchase pets, and automate gameplay. It includes:

A main UI with stats (currency, weapon, area) and navigation buttons (Shop, Areas, Upgrades, Pets, Auto Cut).
Sub-menus for purchasing weapons, unlocking areas, upgrading stats, and buying pets.
Combat mechanics to "cut" BrainRot objects within range.
Auto-cut functionality for idle gameplay.
Mobile support with on-screen buttons.
Notifications for in-game events (e.g., critical hits, purchases).
Strengths
Modular Design:

The script is well-organized into functions for creating UI elements (createMainUI, createShopUI, etc.), handling combat (performCut, findNearestBrainRot), and managing game state (updateMainUI, toggleAutoCut).
This modularity makes it easy to maintain and extend.
User Experience:

The UI is visually appealing with consistent use of colors, fonts (Gotham), and scaling (TextScaled).
Notifications provide feedback for player actions, enhancing engagement.
Mobile support ensures accessibility across devices.
Error Handling:

The script uses pcall to handle potential errors in remote event handling, preventing crashes.
It includes retry logic in initializeGame to handle network issues when fetching player data.
Performance Considerations:

The script uses Debris to clean up temporary UI elements (notifications).
It avoids unnecessary UI updates by checking for existing elements before creating new ones.
Gameplay Features:

The combat system (findNearestBrainRot, performCut) is simple but effective, with range-based targeting.
The auto-cut feature supports idle gameplay, a staple of simulator games.
The upgrade and progression systems (weapons, areas, pets) are well-implemented, with clear cost scaling and ownership tracking.
Potential Issues
Missing UI Corner Rounding:

The UI elements (frames, buttons) use sharp corners, which can look dated compared to modern UIs with rounded edges. Adding UICorner components would improve aesthetics.
Hardcoded Values:

Many UI sizes, positions, and colors are hardcoded (e.g., UDim2.new(0, 600, 0, 500)). This makes it harder to adjust the layout for different screen sizes or themes.
Auto-Cut Performance:

The auto-cut feature uses RunService.Heartbeat, which runs every frame. This could be optimized to run less frequently (e.g., every 0.1 seconds) to reduce client-side load, especially on low-end devices.
Lack of UI Animations:

The UI lacks animations (e.g., button hover effects, frame transitions), which could make the game feel more polished.
Error Handling in UI Creation:

While remote event handling uses pcall, UI creation functions do not. If a UI element fails to create (e.g., due to parenting issues), it could break the script.
Mobile Control Overlap:

The mobile cut button is positioned at the bottom-right corner, which might overlap with other UI elements (e.g., the auto-cut toggle button). This could confuse mobile players.
Debugging Output:

The script includes print statements for debugging, but these could clutter the output log in a production environment. Consider using a logging system or disabling them in release builds.
Data Validation:

The script assumes server responses (UpdateUI events) are valid. Malicious clients could exploit this by sending fake data. While client-side validation is limited, basic checks (e.g., ensuring data.newBalance is a number) could improve robustness.


-- CUT BRAINROT SIMULATOR - CLIENT SCRIPT
-- Place this in StarterPlayer > StarterPlayerScripts

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

-- Wait for remotes
local remotes = {}
local remoteNames = {
    "CutBrainRot", "UpgradeWeapon", "UpgradeSpeed", "UnlockArea", "SellBrainRot",
    "UpdateUI", "GetPlayerData", "AutoCut", "BuyPet", "EquipWeapon"
}

for _, name in pairs(remoteNames) do
    remotes[name] = ReplicatedStorage:WaitForChild(name, 10)
end

-- Variables
local screenGui
local playerData
local gameConfig
local isCutting = false
local autoCutEnabled = false
local autoCutConnection

-- Utility Functions
local function formatNumber(num)
    if num >= 1e12 then
        return string.format("%.2fT", num / 1e12)
    elseif num >= 1e9 then
        return string.format("%.2fB", num / 1e9)
    elseif num >= 1e6 then
        return string.format("%.2fM", num / 1e6)
    elseif num >= 1e3 then
        return string.format("%.2fK", num / 1e3)
    else
        return tostring(math.floor(num))
    end
end

local function getRarityColor(rarity)
    local colors = {
        Common = Color3.fromRGB(150, 150, 150),
        Rare = Color3.fromRGB(50, 150, 255),
        Epic = Color3.fromRGB(150, 50, 255),
        Legendary = Color3.fromRGB(255, 215, 0),
        Mythic = Color3.fromRGB(255, 50, 150)
    }
    return colors[rarity] or colors.Common
end

-- Combat System
local function findNearestBrainRot()
    local character = player.Character
    if not character or not character:FindFirstChild("HumanoidRootPart") then return nil end
    
    local playerPosition = character.HumanoidRootPart.Position
    local weapon = gameConfig and gameConfig.Weapons[playerData and playerData.EquippedWeapon or 1]
    local range = weapon and weapon.Range or 5
    
    local nearestBrainRot = nil
    local nearestDistance = range
    
    for _, obj in pairs(workspace:GetChildren()) do
        if obj.Name:find("BrainRot_") and obj:IsA("Part") then
            local distance = (obj.Position - playerPosition).Magnitude
            if distance <= nearestDistance then
                nearestBrainRot = obj
                nearestDistance = distance
            end
        end
    end
    
    return nearestBrainRot
end

local function performCut()
    if isCutting then return end
    isCutting = true
    
    local nearestBrainRot = findNearestBrainRot()
    if nearestBrainRot then
        remotes.CutBrainRot:FireServer(nearestBrainRot)
    end
    
    wait(0.1)
    isCutting = false
end

-- Create Main UI
local function createMainUI()
    screenGui = Instance.new("ScreenGui")
    screenGui.Name = "CutBrainRotUI"
    screenGui.ResetOnSpawn = false
    screenGui.Parent = playerGui
    
    -- Top Stats Bar
    local topBar = Instance.new("Frame")
    topBar.Name = "TopBar"
    topBar.Size = UDim2.new(1, 0, 0, 80)
    topBar.Position = UDim2.new(0, 0, 0, 0)
    topBar.BackgroundColor3 = Color3.fromRGB(30, 30, 40)
    topBar.BorderSizePixel = 0
    topBar.Parent = screenGui
    
    -- Money Display
    local moneyFrame = Instance.new("Frame")
    moneyFrame.Size = UDim2.new(0, 250, 1, 0)
    moneyFrame.Position = UDim2.new(0, 10, 0, 0)
    moneyFrame.BackgroundColor3 = Color3.fromRGB(50, 50, 60)
    moneyFrame.BorderSizePixel = 0
    moneyFrame.Parent = topBar
    
    local moneyLabel = Instance.new("TextLabel")
    moneyLabel.Name = "MoneyLabel"
    moneyLabel.Size = UDim2.new(1, 0, 1, 0)
    moneyLabel.BackgroundTransparency = 1
    moneyLabel.Text = "🧠 BrainRot: 0"
    moneyLabel.TextColor3 = Color3.fromRGB(255, 100, 255)
    moneyLabel.TextScaled = true
    moneyLabel.Font = Enum.Font.GothamBold
    moneyLabel.Parent = moneyFrame
    
    -- Weapon Display
    local weaponFrame = Instance.new("Frame")
    weaponFrame.Size = UDim2.new(0, 300, 1, 0)
    weaponFrame.Position = UDim2.new(0, 270, 0, 0)
    weaponFrame.BackgroundColor3 = Color3.fromRGB(60, 50, 50)
    weaponFrame.BorderSizePixel = 0
    weaponFrame.Parent = topBar
    
    local weaponLabel = Instance.new("TextLabel")
    weaponLabel.Name = "WeaponLabel"
    weaponLabel.Size = UDim2.new(1, 0, 1, 0)
    weaponLabel.BackgroundTransparency = 1
    weaponLabel.Text = "⚔️ Weapon: Plastic Spoon"
    weaponLabel.TextColor3 = Color3.fromRGB(255, 215, 0)
    weaponLabel.TextScaled = true
    weaponLabel.Font = Enum.Font.GothamBold
    weaponLabel.Parent = weaponFrame
    
    -- Area Display
    local areaFrame = Instance.new("Frame")
    areaFrame.Size = UDim2.new(0, 200, 1, 0)
    areaFrame.Position = UDim2.new(0, 580, 0, 0)
    areaFrame.BackgroundColor3 = Color3.fromRGB(50, 60, 50)
    areaFrame.BorderSizePixel = 0
    areaFrame.Parent = topBar
    
    local areaLabel = Instance.new("TextLabel")
    areaLabel.Name = "AreaLabel"
    areaLabel.Size = UDim2.new(1, 0, 1, 0)
    areaLabel.BackgroundTransparency = 1
    areaLabel.Text = "🌍 Area: Skibidi Toilets"
    areaLabel.TextColor3 = Color3.fromRGB(100, 255, 100)
    areaLabel.TextScaled = true
    areaLabel.Font = Enum.Font.Gotham
    areaLabel.Parent = areaFrame
    
    -- Menu Buttons Frame
    local buttonsFrame = Instance.new("Frame")
    buttonsFrame.Size = UDim2.new(0, 400, 0, 100)
    buttonsFrame.Position = UDim2.new(0, 20, 0, 90)
    buttonsFrame.BackgroundTransparency = 1
    buttonsFrame.Parent = screenGui
    
    -- Shop Button
    local shopButton = Instance.new("TextButton")
    shopButton.Name = "ShopButton"
    shopButton.Size = UDim2.new(0, 90, 0, 80)
    shopButton.Position = UDim2.new(0, 0, 0, 0)
    shopButton.BackgroundColor3 = Color3.fromRGB(50, 150, 50)
    shopButton.Text = "🛒\nSHOP"
    shopButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    shopButton.TextScaled = true
    shopButton.Font = Enum.Font.GothamBold
    shopButton.BorderSizePixel = 0
    shopButton.Parent = buttonsFrame
    
    -- Areas Button
    local areasButton = Instance.new("TextButton")
    areasButton.Name = "AreasButton"
    areasButton.Size = UDim2.new(0, 90, 0, 80)
    areasButton.Position = UDim2.new(0, 100, 0, 0)
    areasButton.BackgroundColor3 = Color3.fromRGB(50, 50, 150)
    areasButton.Text = "🗺️\nAREAS"
    areasButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    areasButton.TextScaled = true
    areasButton.Font = Enum.Font.GothamBold
    areasButton.BorderSizePixel = 0
    areasButton.Parent = buttonsFrame
    
    -- Upgrades Button
    local upgradesButton = Instance.new("TextButton")
    upgradesButton.Name = "UpgradesButton"
    upgradesButton.Size = UDim2.new(0, 90, 0, 80)
    upgradesButton.Position = UDim2.new(0, 200, 0, 0)
    upgradesButton.BackgroundColor3 = Color3.fromRGB(150, 50, 150)
    upgradesButton.Text = "⬆️\nUPGRADES"
    upgradesButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    upgradesButton.TextScaled = true
    upgradesButton.Font = Enum.Font.GothamBold
    upgradesButton.BorderSizePixel = 0
    upgradesButton.Parent = buttonsFrame
    
    -- Pets Button
    local petsButton = Instance.new("TextButton")
    petsButton.Name = "PetsButton"
    petsButton.Size = UDim2.new(0, 90, 0, 80)
    petsButton.Position = UDim2.new(0, 300, 0, 0)
    petsButton.BackgroundColor3 = Color3.fromRGB(255, 150, 50)
    petsButton.Text = "🐾\nPETS"
    petsButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    petsButton.TextScaled = true
    petsButton.Font = Enum.Font.GothamBold
    petsButton.BorderSizePixel = 0
    petsButton.Parent = buttonsFrame
    
    -- Auto Cut Button
    local autoCutButton = Instance.new("TextButton")
    autoCutButton.Name = "AutoCutButton"
    autoCutButton.Size = UDim2.new(0, 120, 0, 50)
    autoCutButton.Position = UDim2.new(1, -140, 0, 90)
    autoCutButton.BackgroundColor3 = Color3.fromRGB(100, 100, 100)
    autoCutButton.Text = "🤖 AUTO CUT: OFF"
    autoCutButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    autoCutButton.TextScaled = true
    autoCutButton.Font = Enum.Font.Gotham
    autoCutButton.BorderSizePixel = 0
    autoCutButton.Parent = screenGui
    
    -- Button Events
    shopButton.MouseButton1Click:Connect(function()
        createShopUI()
    end)
    
    areasButton.MouseButton1Click:Connect(function()
        createAreasUI()
    end)
    
    upgradesButton.MouseButton1Click:Connect(function()
        createUpgradesUI()
    end)
    
    petsButton.MouseButton1Click:Connect(function()
        createPetsUI()
    end)
    
    autoCutButton.MouseButton1Click:Connect(function()
        toggleAutoCut()
        if autoCutEnabled then
            autoCutButton.BackgroundColor3 = Color3.fromRGB(50, 150, 50)
            autoCutButton.Text = "🤖 AUTO CUT: ON"
        else
            autoCutButton.BackgroundColor3 = Color3.fromRGB(100, 100, 100)
            autoCutButton.Text = "🤖 AUTO CUT: OFF"
        end
    end)
end

-- Create Shop UI
local function createShopUI()
    -- Close any existing shop UI
    local existingShop = screenGui:FindFirstChild("ShopFrame")
    if existingShop then
        existingShop:Destroy()
    end
    
    local shopFrame = Instance.new("Frame")
    shopFrame.Name = "ShopFrame"
    shopFrame.Size = UDim2.new(0, 600, 0, 500)
    shopFrame.Position = UDim2.new(0.5, -300, 0.5, -250)
    shopFrame.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
    shopFrame.BorderSizePixel = 0
    shopFrame.Parent = screenGui
    
    local title = Instance.new("TextLabel")
    title.Size = UDim2.new(1, 0, 0, 50)
    title.BackgroundColor3 = Color3.fromRGB(50, 150, 50)
    title.Text = "🛒 WEAPON SHOP"
    title.TextColor3 = Color3.fromRGB(255, 255, 255)
    title.TextScaled = true
    title.Font = Enum.Font.GothamBold
    title.Parent = shopFrame
    
    local closeButton = Instance.new("TextButton")
    closeButton.Size = UDim2.new(0, 40, 0, 40)
    closeButton.Position = UDim2.new(1, -45, 0, 5)
    closeButton.BackgroundColor3 = Color3.fromRGB(200, 50, 50)
    closeButton.Text = "✕"
    closeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    closeButton.TextScaled = true
    closeButton.Font = Enum.Font.GothamBold
    closeButton.BorderSizePixel = 0
    closeButton.Parent = shopFrame
    
    local scrollFrame = Instance.new("ScrollingFrame")
    scrollFrame.Size = UDim2.new(1, -20, 1, -70)
    scrollFrame.Position = UDim2.new(0, 10, 0, 60)
    scrollFrame.BackgroundColor3 = Color3.fromRGB(15, 15, 25)
    scrollFrame.BorderSizePixel = 0
    scrollFrame.ScrollBarThickness = 8
    scrollFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
    scrollFrame.Parent = shopFrame
    
    -- Populate shop with weapons
    if gameConfig and gameConfig.Weapons then
        local yOffset = 10
        for i, weapon in pairs(gameConfig.Weapons) do
            local isOwned = false
            if playerData and playerData.OwnedWeapons then
                for _, ownedWeapon in pairs(playerData.OwnedWeapons) do
                    if ownedWeapon == i then
                        isOwned = true
                        break
                    end
                end
            end
            
            local weaponFrame = Instance.new("Frame")
            weaponFrame.Size = UDim2.new(1, -20, 0, 100)
            weaponFrame.Position = UDim2.new(0, 10, 0, yOffset)
            weaponFrame.BackgroundColor3 = getRarityColor(weapon.Rarity)
            weaponFrame.BorderSizePixel = 0
            weaponFrame.Parent = scrollFrame
            
            local nameLabel = Instance.new("TextLabel")
            nameLabel.Size = UDim2.new(0.4, 0, 0.4, 0)
            nameLabel.Position = UDim2.new(0, 10, 0, 5)
            nameLabel.BackgroundTransparency = 1
            nameLabel.Text = weapon.Name
            nameLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
            nameLabel.TextScaled = true
            nameLabel.Font = Enum.Font.GothamBold
            nameLabel.TextXAlignment = Enum.TextXAlignment.Left
            nameLabel.Parent = weaponFrame
            
            local statsLabel = Instance.new("TextLabel")
            statsLabel.Size = UDim2.new(0.4, 0, 0.6, 0)
            statsLabel.Position = UDim2.new(0, 10, 0.4, 0)
            statsLabel.BackgroundTransparency = 1
            statsLabel.Text = string.format("⚔️ DMG: %d\n📏 Range: %d\n⚡ Speed: %.1fx", weapon.Damage, weapon.Range, weapon.Speed)
            statsLabel.TextColor3 = Color3.fromRGB(200, 200, 200)
            statsLabel.TextSize = 14
            statsLabel.Font = Enum.Font.Gotham
            statsLabel.TextXAlignment = Enum.TextXAlignment.Left
            statsLabel.Parent = weaponFrame
            
            local buyButton = Instance.new("TextButton")
            buyButton.Size = UDim2.new(0.25, 0, 0.6, 0)
            buyButton.Position = UDim2.new(0.7, 0, 0.2, 0)
            buyButton.BorderSizePixel = 0
            buyButton.TextScaled = true
            buyButton.Font = Enum.Font.GothamBold
            
            if isOwned then
                buyButton.BackgroundColor3 = Color3.fromRGB(50, 150, 50)
                buyButton.Text = "OWNED"
                buyButton.TextColor3 = Color3.fromRGB(255, 255, 255)
            else
                buyButton.BackgroundColor3 = Color3.fromRGB(200, 100, 0)
                buyButton.Text = formatNumber(weapon.Cost)
                buyButton.TextColor3 = Color3.fromRGB(255, 255, 255)
                
                buyButton.MouseButton1Click:Connect(function()
                    if playerData and playerData.BrainRot >= weapon.Cost then
                        remotes.UpgradeWeapon:FireServer(i)
                        wait(0.1)
                        shopFrame:Destroy()
                        createShopUI() -- Refresh shop
                    end
                end)
            end
            
            buyButton.Parent = weaponFrame
            yOffset = yOffset + 110
        end
        
        scrollFrame.CanvasSize = UDim2.new(0, 0, 0, yOffset)
    end
    
    closeButton.MouseButton1Click:Connect(function()
        shopFrame:Destroy()
    end)
end

-- Create Areas UI
local function createAreasUI()
    local existingAreas = screenGui:FindFirstChild("AreasFrame")
    if existingAreas then
        existingAreas:Destroy()
    end
    
    local areasFrame = Instance.new("Frame")
    areasFrame.Name = "AreasFrame"
    areasFrame.Size = UDim2.new(0, 700, 0, 400)
    areasFrame.Position = UDim2.new(0.5, -350, 0.5, -200)
    areasFrame.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
    areasFrame.BorderSizePixel = 0
    areasFrame.Parent = screenGui
    
    local title = Instance.new("TextLabel")
    title.Size = UDim2.new(1, 0, 0, 50)
    title.BackgroundColor3 = Color3.fromRGB(50, 50, 150)
    title.Text = "🗺️ BRAINROT AREAS"
    title.TextColor3 = Color3.fromRGB(255, 255, 255)
    title.TextScaled = true
    title.Font = Enum.Font.GothamBold
    title.Parent = areasFrame
    
    local closeButton = Instance.new("TextButton")
    closeButton.Size = UDim2.new(0, 40, 0, 40)
    closeButton.Position = UDim2.new(1, -45, 0, 5)
    closeButton.BackgroundColor3 = Color3.fromRGB(200, 50, 50)
    closeButton.Text = "✕"
    closeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    closeButton.TextScaled = true
    closeButton.Font = Enum.Font.GothamBold
    closeButton.BorderSizePixel = 0
    closeButton.Parent = areasFrame
    
    local contentFrame = Instance.new("Frame")
    contentFrame.Size = UDim2.new(1, -20, 1, -70)
    contentFrame.Position = UDim2.new(0, 10, 0, 60)
    contentFrame.BackgroundTransparency = 1
    contentFrame.Parent = areasFrame
    
    -- Populate areas
    if gameConfig and gameConfig.Areas then
        for i, area in pairs(gameConfig.Areas) do
            local isUnlocked = false
            if playerData and playerData.UnlockedAreas then
                for _, unlockedArea in pairs(playerData.UnlockedAreas) do
                    if unlockedArea == i then
                        isUnlocked = true
                        break
                    end
                end
            end
            
            local isCurrent = playerData and playerData.CurrentArea == i
            
            local areaFrame = Instance.new("Frame")
            areaFrame.Size = UDim2.new(0.48, 0, 0.45, 0)
            areaFrame.Position = UDim2.new(((i-1) % 2) * 0.52, 0, math.floor((i-1) / 2) * 0.52, 0)
            areaFrame.BackgroundColor3 = isCurrent and Color3.fromRGB(100, 255, 100) or (isUnlocked and area.Color or Color3.fromRGB(50, 50, 50))
            areaFrame.BorderSizePixel = 0
            areaFrame.Parent = contentFrame
            
            local nameLabel = Instance.new("TextLabel")
            nameLabel.Size = UDim2.new(1, 0, 0.3, 0)
            nameLabel.BackgroundTransparency = 1
            nameLabel.Text = area.Name
            nameLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
            nameLabel.TextScaled = true
            nameLabel.Font = Enum.Font.GothamBold
            nameLabel.Parent = areaFrame
            
            local infoLabel = Instance.new("TextLabel")
            infoLabel.Size = UDim2.new(1, 0, 0.4, 0)
            infoLabel.Position = UDim2.new(0, 0, 0.3, 0)
            infoLabel.BackgroundTransparency = 1
            infoLabel.Text = string.format("Base Value: %dx\nMultiplier: %dx", area.BaseValue, area.Multiplier)
            infoLabel.TextColor3 = Color3.fromRGB(200, 200, 200)
            infoLabel.TextSize = 14
            infoLabel.Font = Enum.Font.Gotham
            infoLabel.Parent = areaFrame
            
            local actionButton = Instance.new("TextButton")
            actionButton.Size = UDim2.new(0.8, 0, 0.25, 0)
            actionButton.Position = UDim2.new(0.1, 0, 0.7, 0)
            actionButton.BorderSizePixel = 0
            actionButton.TextScaled = true
            actionButton.Font = Enum.Font.GothamBold
            
            if isCurrent then
                actionButton.BackgroundColor3 = Color3.fromRGB(100, 255, 100)
                actionButton.Text = "CURRENT AREA"
                actionButton.TextColor3 = Color3.fromRGB(0, 0, 0)
            elseif isUnlocked then
                actionButton.BackgroundColor3 = Color3.fromRGB(50, 150, 255)
                actionButton.Text = "TRAVEL HERE"
                actionButton.TextColor3 = Color3.fromRGB(255, 255, 255)
                
                actionButton.MouseButton1Click:Connect(function()
                    if playerData then
                        playerData.CurrentArea = i
                        if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
                            player.Character.HumanoidRootPart.CFrame = CFrame.new(area.Position + Vector3.new(0, 5, 0))
                        end
                        areasFrame:Destroy()
                        updateMainUI()
                    end
                end)
            else
                actionButton.BackgroundColor3 = Color3.fromRGB(200, 100, 0)
                actionButton.Text = "UNLOCK: " .. formatNumber(area.UnlockCost)
                actionButton.TextColor3 = Color3.fromRGB(255, 255, 255)
                
                if area.UnlockCost > 0 then
                    actionButton.MouseButton1Click:Connect(function()
                        if playerData and playerData.BrainRot >= area.UnlockCost then
                            remotes.UnlockArea:FireServer(i)
                            wait(0.1)
                            areasFrame:Destroy()
                            createAreasUI()
                        end
                    end)
                end
            end
            
            actionButton.Parent = areaFrame
        end
    end
    
    closeButton.MouseButton1Click:Connect(function()
        areasFrame:Destroy()
    end)
end

-- Create Upgrades UI
local function createUpgradesUI()
    local existingUpgrades = screenGui:FindFirstChild("UpgradesFrame")
    if existingUpgrades then
        existingUpgrades:Destroy()
    end
    
    local upgradesFrame = Instance.new("Frame")
    upgradesFrame.Name = "UpgradesFrame"
    upgradesFrame.Size = UDim2.new(0, 600, 0, 500)
    upgradesFrame.Position = UDim2.new(0.5, -300, 0.5, -250)
    upgradesFrame.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
    upgradesFrame.BorderSizePixel = 0
    upgradesFrame.Parent = screenGui
    
    local title = Instance.new("TextLabel")
    title.Size = UDim2.new(1, 0, 0, 50)
    title.BackgroundColor3 = Color3.fromRGB(150, 50, 150)
    title.Text = "⬆️ UPGRADES"
    title.TextColor3 = Color3.fromRGB(255, 255, 255)
    title.TextScaled = true
    title.Font = Enum.Font.GothamBold
    title.Parent = upgradesFrame
    
    local closeButton = Instance.new("TextButton")
    closeButton.Size = UDim2.new(0, 40, 0, 40)
    closeButton.Position = UDim2.new(1, -45, 0, 5)
    closeButton.BackgroundColor3 = Color3.fromRGB(200, 50, 50)
    closeButton.Text = "✕"
    closeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    closeButton.TextScaled = true
    closeButton.Font = Enum.Font.GothamBold
    closeButton.BorderSizePixel = 0
    closeButton.Parent = upgradesFrame
    
    local scrollFrame = Instance.new("ScrollingFrame")
    scrollFrame.Size = UDim2.new(1, -20, 1, -70)
    scrollFrame.Position = UDim2.new(0, 10, 0, 60)
    scrollFrame.BackgroundColor3 = Color3.fromRGB(15, 15, 25)
    scrollFrame.BorderSizePixel = 0
    scrollFrame.ScrollBarThickness = 8
    scrollFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
    scrollFrame.Parent = upgradesFrame
    
    -- Populate upgrades
    if gameConfig and gameConfig.Upgrades then
        local yOffset = 10
        for upgradeName, upgradeConfig in pairs(gameConfig.Upgrades) do
            local currentLevel = (playerData and playerData.Upgrades and playerData.Upgrades[upgradeName]) or 1
            local cost = math.floor(upgradeConfig.BaseCost * (upgradeConfig.Multiplier ^ (currentLevel - 1)))
            local isMaxLevel = currentLevel >= upgradeConfig.MaxLevel
            
            local upgradeFrame = Instance.new("Frame")
            upgradeFrame.Size = UDim2.new(1, -20, 0, 80)
            upgradeFrame.Position = UDim2.new(0, 10, 0, yOffset)
            upgradeFrame.BackgroundColor3 = Color3.fromRGB(40, 40, 50)
            upgradeFrame.BorderSizePixel = 0
            upgradeFrame.Parent = scrollFrame
            
            local nameLabel = Instance.new("TextLabel")
            nameLabel.Size = UDim2.new(0.3, 0, 0.5, 0)
            nameLabel.Position = UDim2.new(0, 10, 0, 5)
            nameLabel.BackgroundTransparency = 1
            nameLabel.Text = upgradeName
            nameLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
            nameLabel.TextScaled = true
            nameLabel.Font = Enum.Font.GothamBold
            nameLabel.TextXAlignment = Enum.TextXAlignment.Left
            nameLabel.Parent = upgradeFrame
            
            local levelLabel = Instance.new("TextLabel")
            levelLabel.Size = UDim2.new(0.3, 0, 0.4, 0)
            levelLabel.Position = UDim2.new(0, 10, 0.5, 0)
            levelLabel.BackgroundTransparency = 1
            levelLabel.Text = string.format("Level: %d/%d", currentLevel, upgradeConfig.MaxLevel)
            levelLabel.TextColor3 = Color3.fromRGB(200, 200, 200)
            levelLabel.TextSize = 14
            levelLabel.Font = Enum.Font.Gotham
            levelLabel.TextXAlignment = Enum.TextXAlignment.Left
            levelLabel.Parent = upgradeFrame
            
            local upgradeButton = Instance.new("TextButton")
            upgradeButton.Size = UDim2.new(0.25, 0, 0.6, 0)
            upgradeButton.Position = UDim2.new(0.7, 0, 0.2, 0)
            upgradeButton.BorderSizePixel = 0
            upgradeButton.TextScaled = true
            upgradeButton.Font = Enum.Font.GothamBold
            
            if isMaxLevel then
                upgradeButton.BackgroundColor3 = Color3.fromRGB(100, 100, 100)
                upgradeButton.Text = "MAX LEVEL"
                upgradeButton.TextColor3 = Color3.fromRGB(200, 200, 200)
            else
                upgradeButton.BackgroundColor3 = Color3.fromRGB(150, 50, 150)
                upgradeButton.Text = formatNumber(cost)
                upgradeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
                
                upgradeButton.MouseButton1Click:Connect(function()
                    if playerData and playerData.BrainRot >= cost then
                        remotes.UpgradeSpeed:FireServer(upgradeName)
                        wait(0.1)
                        upgradesFrame:Destroy()
                        createUpgradesUI()
                    end
                end)
            end
            
            upgradeButton.Parent = upgradeFrame
            yOffset = yOffset + 90
        end
        
        scrollFrame.CanvasSize = UDim2.new(0, 0, 0, yOffset)
    end
    
    closeButton.MouseButton1Click:Connect(function()
        upgradesFrame:Destroy()
    end)
end

-- Create Pets UI
local function createPetsUI()
    local existingPets = screenGui:FindFirstChild("PetsFrame")
    if existingPets then
        existingPets:Destroy()
    end
    
    local petsFrame = Instance.new("Frame")
    petsFrame.Name = "PetsFrame"
    petsFrame.Size = UDim2.new(0, 600, 0, 500)
    petsFrame.Position = UDim2.new(0.5, -300, 0.5, -250)
    petsFrame.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
    petsFrame.BorderSizePixel = 0
    petsFrame.Parent = screenGui
    
    local title = Instance.new("TextLabel")
    title.Size = UDim2.new(1, 0, 0, 50)
    title.BackgroundColor3 = Color3.fromRGB(255, 150, 50)
    title.Text = "🐾 BRAINROT PETS"
    title.TextColor3 = Color3.fromRGB(255, 255, 255)
    title.TextScaled = true
    title.Font = Enum.Font.GothamBold
    title.Parent = petsFrame
    
    local closeButton = Instance.new("TextButton")
    closeButton.Size = UDim2.new(0, 40, 0, 40)
    closeButton.Position = UDim2.new(1, -45, 0, 5)
    closeButton.BackgroundColor3 = Color3.fromRGB(200, 50, 50)
    closeButton.Text = "✕"
    closeButton.TextColor3 = Color3.fromRGB(255, 255, 255)
    closeButton.TextScaled = true
    closeButton.Font = Enum.Font.GothamBold
    closeButton.BorderSizePixel = 0
    closeButton.Parent = petsFrame
    
    local scrollFrame = Instance.new("ScrollingFrame")
    scrollFrame.Size = UDim2.new(1, -20, 1, -70)
    scrollFrame.Position = UDim2.new(0, 10, 0, 60)
    scrollFrame.BackgroundColor3 = Color3.fromRGB(15, 15, 25)
    scrollFrame.BorderSizePixel = 0
    scrollFrame.ScrollBarThickness = 8
    scrollFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
    scrollFrame.Parent = petsFrame
    
    -- Populate pets
    if gameConfig and gameConfig.Pets then
        local yOffset = 10
        for i, pet in pairs(gameConfig.Pets) do
            local isOwned = false
            if playerData and playerData.OwnedPets then
                for _, ownedPet in pairs(playerData.OwnedPets) do
                    if ownedPet == i then
                        isOwned = true
                        break
                    end
                end
            end
            
            local petFrame = Instance.new("Frame")
            petFrame.Size = UDim2.new(1, -20, 0, 100)
            petFrame.Position = UDim2.new(0, 10, 0, yOffset)
            petFrame.BackgroundColor3 = getRarityColor(pet.Rarity)
            petFrame.BorderSizePixel = 0
            petFrame.Parent = scrollFrame
            
            local nameLabel = Instance.new("TextLabel")
            nameLabel.Size = UDim2.new(0.4, 0, 0.4, 0)
            nameLabel.Position = UDim2.new(0, 10, 0, 5)
            nameLabel.BackgroundTransparency = 1
            nameLabel.Text = pet.Name
            nameLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
            nameLabel.TextScaled = true
            nameLabel.Font = Enum.Font.GothamBold
            nameLabel.TextXAlignment = Enum.TextXAlignment.Left
            nameLabel.Parent = petFrame
            
            local statsLabel = Instance.new("TextLabel")
            statsLabel.Size = UDim2.new(0.4, 0, 0.6, 0)
            statsLabel.Position = UDim2.new(0, 10, 0.4, 0)
            statsLabel.BackgroundTransparency = 1
            statsLabel.Text = string.format("💰 %.1fx Multiplier\n🤖 %.0f%% Auto Cut", pet.Multiplier, pet.AutoCutChance * 100)
            statsLabel.TextColor3 = Color3.fromRGB(200, 200, 200)
            statsLabel.TextSize = 14
            statsLabel.Font = Enum.Font.Gotham
            statsLabel.TextXAlignment = Enum.TextXAlignment.Left
            statsLabel.Parent = petFrame
            
            local buyButton = Instance.new("TextButton")
            buyButton.Size = UDim2.new(0.25, 0, 0.6, 0)
            buyButton.Position = UDim2.new(0.7, 0, 0.2, 0)
            buyButton.BorderSizePixel = 0
            buyButton.TextScaled = true
            buyButton.Font = Enum.Font.GothamBold
            
            if isOwned then
                buyButton.BackgroundColor3 = Color3.fromRGB(50, 150, 50)
                buyButton.Text = "OWNED"
                buyButton.TextColor3 = Color3.fromRGB(255, 255, 255)
            else
                buyButton.BackgroundColor3 = Color3.fromRGB(255, 150, 50)
                buyButton.Text = formatNumber(pet.Cost)
                buyButton.TextColor3 = Color3.fromRGB(255, 255, 255)
                
                buyButton.MouseButton1Click:Connect(function()
                    if playerData and playerData.BrainRot >= pet.Cost then
                        remotes.BuyPet:FireServer(i)
                        wait(0.1)
                        petsFrame:Destroy()
                        createPetsUI()
                    end
                end)
            end
            
            buyButton.Parent = petFrame
            yOffset = yOffset + 110
        end
        
        scrollFrame.CanvasSize = UDim2.new(0, 0, 0, yOffset)
    end
    
    closeButton.MouseButton1Click:Connect(function()
        petsFrame:Destroy()
    end)
end

-- Auto-cut toggle
local function toggleAutoCut()
    autoCutEnabled = not autoCutEnabled
    
    if autoCutEnabled then
        if autoCutConnection then autoCutConnection:Disconnect() end
        autoCutConnection = RunService.Heartbeat:Connect(function()
            if playerData and player.Parent then
                performCut()
            end
        end)
    else
        if autoCutConnection then
            autoCutConnection:Disconnect()
            autoCutConnection = nil
        end
    end
end

-- Update main UI displays
local function updateMainUI()
    if not playerData or not gameConfig or not screenGui then return end
    
    local moneyLabel = screenGui:FindFirstChild("TopBar") and screenGui.TopBar:FindFirstChild("MoneyLabel")
    local weaponLabel = screenGui:FindFirstChild("TopBar") and screenGui.TopBar:FindFirstChild("WeaponLabel")
    local areaLabel = screenGui:FindFirstChild("TopBar") and screenGui.TopBar:FindFirstChild("AreaLabel")
    
    if moneyLabel then
        moneyLabel.Text = "🧠 BrainRot: " .. formatNumber(playerData.BrainRot)
    end
    
    if weaponLabel then
        local weapon = gameConfig.Weapons[playerData.EquippedWeapon]
        weaponLabel.Text = "⚔️ Weapon: " .. (weapon and weapon.Name or "Plastic Spoon")
    end
    
    if areaLabel then
        local area = gameConfig.Areas[playerData.CurrentArea]
        areaLabel.Text = "🌍 Area: " .. (area and area.Name or "Skibidi Toilets")
    end
end

-- Notification system
local function showNotification(text, color)
    local notifGui = Instance.new("ScreenGui")
    notifGui.Name = "Notification"
    notifGui.Parent = playerGui
    
    local notifFrame = Instance.new("Frame")
    notifFrame.Size = UDim2.new(0, 250, 0, 60)
    notifFrame.Position = UDim2.new(0.5, -125, 0, 200)
    notifFrame.BackgroundColor3 = color or Color3.fromRGB(50, 200, 50)
    notifFrame.BorderSizePixel = 0
    notifFrame.Parent = notifGui
    
    local notifLabel = Instance.new("TextLabel")
    notifLabel.Size = UDim2.new(1, 0, 1, 0)
    notifLabel.BackgroundTransparency = 1
    notifLabel.Text = text
    notifLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
    notifLabel.TextScaled = true
    notifLabel.Font = Enum.Font.GothamBold
    notifLabel.Parent = notifFrame
    
    Debris:AddItem(notifGui, 3)
end

-- Input handling
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        performCut()
    elseif input.KeyCode == Enum.KeyCode.E then
        performCut()
    elseif input.KeyCode == Enum.KeyCode.Q then
        toggleAutoCut()
    end
end)

-- Mobile support
if UserInputService.TouchEnabled then
    coroutine.wrap(function()
        while not screenGui do
            wait(0.1)
        end
        
        local mobileFrame = Instance.new("Frame")
        mobileFrame.Name = "MobileControls"
        mobileFrame.Size = UDim2.new(0, 120, 0, 120)
        mobileFrame.Position = UDim2.new(1, -140, 1, -140)
        mobileFrame.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
        mobileFrame.BorderSizePixel = 0
        mobileFrame.Parent = screenGui
        
        local cutButton = Instance.new("TextButton")
        cutButton.Size = UDim2.new(1, 0, 1, 0)
        cutButton.BackgroundColor3 = Color3.fromRGB(255, 100, 100)
        cutButton.Text = "✂️\nCUT"
        cutButton.TextColor3 = Color3.fromRGB(255, 255, 255)
        cutButton.TextScaled = true
        cutButton.Font = Enum.Font.GothamBold
        cutButton.BorderSizePixel = 0
        cutButton.Parent = mobileFrame
        
        cutButton.MouseButton1Click:Connect(performCut)
    end)()
end

-- Remote event handlers
remotes.UpdateUI.OnClientEvent:Connect(function(action, data)
    print("Received UpdateUI event:", action)
    
    pcall(function()
        if action == "Initialize" then
            print("Initializing with data:", data.playerData and "PlayerData received" or "No PlayerData")
            playerData = data.playerData
            gameConfig = data.gameConfig
            
            if not screenGui then
                createMainUI()
            end
            
            updateMainUI()
            
            print("Player equipped weapon:", playerData.EquippedWeapon)
            if gameConfig and gameConfig.Weapons and gameConfig.Weapons[playerData.EquippedWeapon] then
                print("Weapon name:", gameConfig.Weapons[playerData.EquippedWeapon].Name)
            end
            
        elseif action == "BrainRotCut" then
            if playerData then
                playerData.BrainRot = data.newBalance
                updateMainUI()
                
                local critText = data.isCritical and "CRIT! +" or "+"
                showNotification(critText .. formatNumber(data.reward) .. " BrainRot!", 
                    data.isCritical and Color3.fromRGB(255, 215, 0) or Color3.fromRGB(50, 200, 50))
            end
            
        elseif action == "WeaponPurchased" then
            if playerData then
                playerData.EquippedWeapon = data.weaponIndex
                local alreadyOwned = false
                for _, ownedWeapon in pairs(playerData.OwnedWeapons) do
                    if ownedWeapon == data.weaponIndex then
                        alreadyOwned = true
                        break
                    end
                end
                if not alreadyOwned then
                    table.insert(playerData.OwnedWeapons, data.weaponIndex)
                end
                playerData.BrainRot = data.newBalance
                updateMainUI()
                showNotification("Weapon Purchased!", Color3.fromRGB(0, 150, 255))
            end
            
        elseif action == "UpgradeSuccess" then
            if playerData then
                playerData.Upgrades[data.upgradeType] = data.newLevel
                playerData.BrainRot = data.newBalance
                updateMainUI()
                showNotification(data.upgradeType .. " Upgraded!", Color3.fromRGB(150, 50, 255))
            end
            
        elseif action == "AreaUnlocked" then
            if playerData then
                local alreadyUnlocked = false
                for _, unlockedArea in pairs(playerData.UnlockedAreas) do
                    if unlockedArea == data.areaIndex then
                        alreadyUnlocked = true
                        break
                    end
                end
                if not alreadyUnlocked then
                    table.insert(playerData.UnlockedAreas, data.areaIndex)
                end
                playerData.CurrentArea = data.areaIndex
                playerData.BrainRot = data.newBalance
                updateMainUI()
                showNotification("New Area Unlocked!", Color3.fromRGB(50, 150, 255))
            end
            
        elseif action == "PetPurchased" then
            if playerData then
                local alreadyOwned = false
                for _, ownedPet in pairs(playerData.OwnedPets) do
                    if ownedPet == data.petIndex then
                        alreadyOwned = true
                        break
                    end
                end
                if not alreadyOwned then
                    table.insert(playerData.OwnedPets, data.petIndex)
                    if #playerData.EquippedPets < 3 then
                        table.insert(playerData.EquippedPets, data.petIndex)
                    end
                end
                playerData.BrainRot = data.newBalance
                updateMainUI()
                showNotification("Pet Purchased!", Color3.fromRGB(255, 150, 50))
            end
        end
    end)
end)

-- Initialization
local function initializeGame()
    print("Initializing game...")
    
    if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then
        player.CharacterAdded:Wait()
        wait(3)
    end
    
    print("Requesting player data...")
    
    local maxRetries = 3
    for attempt = 1, maxRetries do
        local success, error = pcall(function()
            remotes.GetPlayerData:FireServer()
        end)
        
        if success then
            print("Successfully requested player data")
            break
        else
            warn("Failed to request player data (attempt " .. attempt .. "): " .. tostring(error))
            if attempt < maxRetries then
                wait(attempt)
            end
        end
    end
end

-- Event connections
player.CharacterAdded:Connect(function()
    wait(3)
    initializeGame()
end)

if player.Character then
    wait(3)
    initializeGame()
end

print("Cut BrainRot Client loaded successfully!")