CCCPaste

[UPD] Dont wake up the brainrots!

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

-- Create UI (Modern style)
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "BrainrotCollectorUI"
screenGui.Parent = player:WaitForChild("PlayerGui")
screenGui.ResetOnSpawn = false

local frame = Instance.new("Frame")
frame.Size = UDim2.new(0, 320, 0, 430) -- Increased for shop button
frame.Position = UDim2.new(0.5, -160, 0.5, -215)
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
frame.BorderSizePixel = 0
frame.Parent = screenGui

local uiGradientFrame = Instance.new("UIGradient")
uiGradientFrame.Color = ColorSequence.new{
    ColorSequenceKeypoint.new(0, Color3.fromRGB(40, 40, 40)),
    ColorSequenceKeypoint.new(1, Color3.fromRGB(20, 20, 20))
}
uiGradientFrame.Parent = frame

local corner = Instance.new("UICorner")
corner.CornerRadius = UDim.new(0, 12)
corner.Parent = frame

local uiStrokeFrame = Instance.new("UIStroke")
uiStrokeFrame.Color = Color3.fromRGB(60, 60, 60)
uiStrokeFrame.Thickness = 1
uiStrokeFrame.Transparency = 0.5
uiStrokeFrame.Parent = frame

-- Make UI draggable
local dragging = false
local dragStartPos, dragStartFramePos
frame.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        dragging = true
        dragStartPos = input.Position
        dragStartFramePos = frame.Position
    end
end)

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

UserInputService.InputChanged:Connect(function(input)
    if dragging and input.UserInputType == Enum.UserInputType.MouseMovement then
        local delta = input.Position - dragStartPos
        frame.Position = UDim2.new(dragStartFramePos.X.Scale, dragStartFramePos.X.Offset + delta.X, dragStartFramePos.Y.Scale, dragStartFramePos.Y.Offset + delta.Y)
    end
end)

local title = Instance.new("TextLabel")
title.Text = "Brainrot Collector"
title.Size = UDim2.new(1, 0, 0, 40)
title.BackgroundTransparency = 1
title.TextColor3 = Color3.fromRGB(255, 255, 255)
title.Font = Enum.Font.GothamBold
title.TextSize = 20
title.Parent = frame

local uiStrokeTitle = Instance.new("UIStroke")
uiStrokeTitle.Color = Color3.fromRGB(100, 100, 100)
uiStrokeTitle.Thickness = 1
uiStrokeTitle.Transparency = 0.8
uiStrokeTitle.Parent = title

local toggleButton = Instance.new("TextButton")
toggleButton.Text = "START"
toggleButton.Size = UDim2.new(0.8, 0, 0, 45)
toggleButton.Position = UDim2.new(0.1, 0, 0.12, 0)
toggleButton.BackgroundColor3 = Color3.fromRGB(76, 175, 80)
toggleButton.TextColor3 = Color3.fromRGB(255, 255, 255)
toggleButton.Font = Enum.Font.GothamBold
toggleButton.TextSize = 16
toggleButton.Parent = frame

local uiGradientToggle = Instance.new("UIGradient")
uiGradientToggle.Color = ColorSequence.new{
    ColorSequenceKeypoint.new(0, Color3.fromRGB(100, 200, 100)),
    ColorSequenceKeypoint.new(1, Color3.fromRGB(50, 150, 50))
}
uiGradientToggle.Parent = toggleButton

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

local uiStrokeToggle = Instance.new("UIStroke")
uiStrokeToggle.Color = Color3.fromRGB(150, 150, 150)
uiStrokeToggle.Thickness = 1
uiStrokeToggle.Transparency = 0.5
uiStrokeToggle.Parent = toggleButton

-- Hover effect for toggleButton
local originalToggleColor = toggleButton.BackgroundColor3
toggleButton.MouseEnter:Connect(function()
    toggleButton.BackgroundColor3 = Color3.fromRGB(100, 200, 100)
end)
toggleButton.MouseLeave:Connect(function()
    toggleButton.BackgroundColor3 = originalToggleColor
end)

local loopButton = Instance.new("TextButton")
loopButton.Text = "LOOP FARM"
loopButton.Size = UDim2.new(0.8, 0, 0, 45)
loopButton.Position = UDim2.new(0.1, 0, 0.24, 0)
loopButton.BackgroundColor3 = Color3.fromRGB(200, 100, 50)
loopButton.TextColor3 = Color3.fromRGB(255, 255, 255)
loopButton.Font = Enum.Font.GothamBold
loopButton.TextSize = 16
loopButton.Parent = frame

local uiGradientLoop = Instance.new("UIGradient")
uiGradientLoop.Color = ColorSequence.new{
    ColorSequenceKeypoint.new(0, Color3.fromRGB(220, 120, 70)),
    ColorSequenceKeypoint.new(1, Color3.fromRGB(180, 80, 30))
}
uiGradientLoop.Parent = loopButton

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

local uiStrokeLoop = Instance.new("UIStroke")
uiStrokeLoop.Color = Color3.fromRGB(150, 150, 150)
uiStrokeLoop.Thickness = 1
uiStrokeLoop.Transparency = 0.5
uiStrokeLoop.Parent = loopButton

-- Hover effect for loopButton
local originalLoopColor = loopButton.BackgroundColor3
loopButton.MouseEnter:Connect(function()
    loopButton.BackgroundColor3 = Color3.fromRGB(220, 120, 70)
end)
loopButton.MouseLeave:Connect(function()
    loopButton.BackgroundColor3 = originalLoopColor
end)

-- STEAL BUTTON
local stealButton = Instance.new("TextButton")
stealButton.Text = "STEAL BRAINROT"
stealButton.Size = UDim2.new(0.8, 0, 0, 45)
stealButton.Position = UDim2.new(0.1, 0, 0.36, 0)
stealButton.BackgroundColor3 = Color3.fromRGB(100, 150, 200)
stealButton.TextColor3 = Color3.fromRGB(255, 255, 255)
stealButton.Font = Enum.Font.GothamBold
stealButton.TextSize = 16
stealButton.Parent = frame

local uiGradientSteal = Instance.new("UIGradient")
uiGradientSteal.Color = ColorSequence.new{
    ColorSequenceKeypoint.new(0, Color3.fromRGB(120, 170, 220)),
    ColorSequenceKeypoint.new(1, Color3.fromRGB(80, 130, 180))
}
uiGradientSteal.Parent = stealButton

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

local uiStrokeSteal = Instance.new("UIStroke")
uiStrokeSteal.Color = Color3.fromRGB(150, 150, 150)
uiStrokeSteal.Thickness = 1
uiStrokeSteal.Transparency = 0.5
uiStrokeSteal.Parent = stealButton

-- Hover effect for stealButton
local originalStealColor = stealButton.BackgroundColor3
stealButton.MouseEnter:Connect(function()
    stealButton.BackgroundColor3 = Color3.fromRGB(120, 170, 220)
end)
stealButton.MouseLeave:Connect(function()
    stealButton.BackgroundColor3 = originalStealColor
end)

-- SHOP BUTTON
local shopButton = Instance.new("TextButton")
shopButton.Text = "GO TO SHOP"
shopButton.Size = UDim2.new(0.8, 0, 0, 45)
shopButton.Position = UDim2.new(0.1, 0, 0.48, 0)
shopButton.BackgroundColor3 = Color3.fromRGB(150, 100, 200)
shopButton.TextColor3 = Color3.fromRGB(255, 255, 255)
shopButton.Font = Enum.Font.GothamBold
shopButton.TextSize = 16
shopButton.Parent = frame

local uiGradientShop = Instance.new("UIGradient")
uiGradientShop.Color = ColorSequence.new{
    ColorSequenceKeypoint.new(0, Color3.fromRGB(170, 120, 220)),
    ColorSequenceKeypoint.new(1, Color3.fromRGB(130, 80, 180))
}
uiGradientShop.Parent = shopButton

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

local uiStrokeShop = Instance.new("UIStroke")
uiStrokeShop.Color = Color3.fromRGB(150, 150, 150)
uiStrokeShop.Thickness = 1
uiStrokeShop.Transparency = 0.5
uiStrokeShop.Parent = shopButton

-- Hover effect for shopButton
local originalShopColor = shopButton.BackgroundColor3
shopButton.MouseEnter:Connect(function()
    shopButton.BackgroundColor3 = Color3.fromRGB(170, 120, 220)
end)
shopButton.MouseLeave:Connect(function()
    shopButton.BackgroundColor3 = originalShopColor
end)

local statusLabel = Instance.new("TextLabel")
statusLabel.Text = "Status: Inactive"
statusLabel.Size = UDim2.new(1, 0, 0, 20)
statusLabel.Position = UDim2.new(0, 0, 0.60, 0)
statusLabel.BackgroundTransparency = 1
statusLabel.TextColor3 = Color3.fromRGB(200, 200, 200)
statusLabel.Font = Enum.Font.Gotham
statusLabel.TextSize = 14
statusLabel.Parent = frame

local countLabel = Instance.new("TextLabel")
countLabel.Text = "Collected: 0"
countLabel.Size = UDim2.new(1, 0, 0, 20)
countLabel.Position = UDim2.new(0, 0, 0.65, 0)
countLabel.BackgroundTransparency = 1
countLabel.TextColor3 = Color3.fromRGB(200, 200, 200)
countLabel.Font = Enum.Font.Gotham
countLabel.TextSize = 14
countLabel.Parent = frame

local debugLabel = Instance.new("TextLabel")
debugLabel.Text = "Debug: Ready"
debugLabel.Size = UDim2.new(1, 0, 0, 80)
debugLabel.Position = UDim2.new(0, 0, 0.70, 0)
debugLabel.BackgroundTransparency = 1
debugLabel.TextColor3 = Color3.fromRGB(255, 165, 0)
debugLabel.Font = Enum.Font.Gotham
debugLabel.TextSize = 12
debugLabel.TextWrapped = true
debugLabel.Parent = frame

local dragLabel = Instance.new("TextLabel")
dragLabel.Text = "Drag to move"
dragLabel.Size = UDim2.new(1, 0, 0, 15)
dragLabel.Position = UDim2.new(0, 0, 0.95, 0)
dragLabel.BackgroundTransparency = 1
dragLabel.TextColor3 = Color3.fromRGB(150, 150, 150)
dragLabel.Font = Enum.Font.Gotham
dragLabel.TextSize = 12
dragLabel.Parent = frame

-- Close button
local closeButton = Instance.new("TextButton")
closeButton.Text = "X"
closeButton.Size = UDim2.new(0, 30, 0, 30)
closeButton.Position = UDim2.new(1, -35, 0, 5)
closeButton.BackgroundTransparency = 1
closeButton.TextColor3 = Color3.fromRGB(255, 100, 100)
closeButton.Font = Enum.Font.GothamBold
closeButton.TextSize = 18
closeButton.Parent = frame
closeButton.MouseButton1Click:Connect(function()
    screenGui:Destroy()
end)

-- Collection variables
local collecting = false
local loopFarming = false
local stealing = false
local collectedCount = 0
local sellFloorPosition = nil
local bodyVelocity = nil
local antiVoidConnection = nil

local function CheckIfItsABrainrot(brainrotmaybe)
    local listofbrainrot = game:GetService("ReplicatedStorage").Brainrots:GetChildren()
    for _, brainrot in ipairs(listofbrainrot) do
        if brainrot.Name == brainrotmaybe then
            return true
        end
    end
    return false
end

-- Function to unequip all tools
local function unequipAllTools()
    local character = player.Character
    if not character then return end
    
    for _, tool in ipairs(character:GetChildren()) do
        if tool:IsA("Tool") then
            tool.Parent = player.Backpack
        end
    end
    debugLabel.Text = "Tools unequipped"
end

-- Anti-void function
local function enableAntiVoid()
    if antiVoidConnection then return end
    
    antiVoidConnection = RunService.Heartbeat:Connect(function()
        local character = player.Character
        if character and character:FindFirstChild("HumanoidRootPart") then
            local hrp = character.HumanoidRootPart
            if hrp.Position.Y < -100 then
                debugLabel.Text = "Anti-void triggered!"
                local spawnLocation = workspace:FindFirstChild("SpawnLocation")
                if spawnLocation then
                    hrp.CFrame = CFrame.new(spawnLocation.Position + Vector3.new(0, 5, 0))
                else
                    hrp.CFrame = CFrame.new(0, 50, 0)
                end
            end
        end
    end)
    debugLabel.Text = "Anti-void enabled"
end

-- Disable anti-void
local function disableAntiVoid()
    if antiVoidConnection then
        antiVoidConnection:Disconnect()
        antiVoidConnection = nil
        debugLabel.Text = "Anti-void disabled"
    end
end

-- Get shop position
local function getShopPosition()
    local shop = workspace:FindFirstChild("Shop")
    if shop then
        return shop.Position + Vector3.new(0, 5, 0)
    end
    
    local shopSign = workspace:FindFirstChild("ShopSign") 
    if shopSign then
        return shopSign.Position + Vector3.new(0, 5, 0)
    end
    
    for _, obj in pairs(workspace:GetChildren()) do
        if string.lower(obj.Name):find("shop") then
            if obj:IsA("BasePart") then
                return obj.Position + Vector3.new(0, 5, 0)
            elseif obj:IsA("Model") and obj.PrimaryPart then
                return obj.PrimaryPart.Position + Vector3.new(0, 5, 0)
            end
        end
    end
    
    return Vector3.new(0, 50, 0) -- Fallback position
end

-- Freeze/unfreeze player using BodyVelocity
local function freezePlayer(freeze)
    local character = player.Character
    if not character or not character:FindFirstChild("HumanoidRootPart") then return end
    local hrp = character.HumanoidRootPart
    
    if freeze then
        if not bodyVelocity then
            bodyVelocity = Instance.new("BodyVelocity")
            bodyVelocity.MaxForce = Vector3.new(4000, 4000, 4000)
            bodyVelocity.Velocity = Vector3.new(0, 0, 0)
            bodyVelocity.Parent = hrp
        end
        debugLabel.Text = "Player frozen"
    else
        if bodyVelocity then
            bodyVelocity:Destroy()
            bodyVelocity = nil
        end
        debugLabel.Text = "Player unfrozen"
    end
end

-- Get sell position
local function getSellPosition()
    if sellFloorPosition then return sellFloorPosition end
    local sellArea = workspace:FindFirstChild("Sell")
    if not sellArea then debugLabel.Text = "Sell area not found" return nil end
    local floorPart = sellArea:FindFirstChild("Floor")
    if not floorPart then debugLabel.Text = "Floor not found" return nil end
    sellFloorPosition = floorPart.Position + Vector3.new(0, 3, 0)
    return sellFloorPosition
end

-- Teleport function
local function teleportTo(position)
    local character = player.Character
    if not character or not character:FindFirstChild("HumanoidRootPart") then return false end
    local success, err = pcall(function()
        character.HumanoidRootPart.CFrame = CFrame.new(position)
    end)
    if not success then debugLabel.Text = "Teleport error: " .. tostring(err) return false end
    return true
end

-- Teleport to sell
local function teleportToSell()
    local pos = getSellPosition()
    if not pos then return false end
    debugLabel.Text = "Teleporting to Sell"
    return teleportTo(pos)
end

-- Search around baseplate
local function searchAroundBaseplate()
    local zonePlate = workspace:FindFirstChild("zone_plate")
    if not zonePlate then debugLabel.Text = "zone_plate not found" return false end
    local baseplate = zonePlate:FindFirstChild("Baseplate")
    if not baseplate then debugLabel.Text = "Baseplate not found" return false end
    debugLabel.Text = "Searching zones..."
    local baseplatePos = baseplate.Position
    local height = 3
    local searchPositions = {}
    
    for row = -1, 1 do
        for col = -1, 1 do
            local x = row * 15
            local z = col * 15
            table.insert(searchPositions, baseplatePos + Vector3.new(x, height, z))
        end
    end
    
    for i = 1, 3 do
        local x = math.random(-20, 20)
        local z = math.random(-20, 20)
        table.insert(searchPositions, baseplatePos + Vector3.new(x, height, z))
    end
    
    for i, pos in ipairs(searchPositions) do
        if not (collecting or loopFarming or stealing) then break end
        debugLabel.Text = "Zone " .. i .. "/" .. #searchPositions
        teleportTo(pos)
        task.wait(0.3)
        local foundBrainrots = findAllBrainrots()
        if #foundBrainrots > 0 then
            debugLabel.Text = "Found " .. #foundBrainrots
            return true
        end
    end
    debugLabel.Text = "No brainrots found"
    return false
end

-- Find brainrots
local function findAllBrainrots()
    local brainrots = {}
    
    local brainrotsFolder = workspace:FindFirstChild("Brainrots")
    if brainrotsFolder then
        for _, obj in ipairs(brainrotsFolder:GetChildren()) do
            if (obj:IsA("BasePart") or obj:IsA("Model")) and obj:FindFirstChildWhichIsA("ProximityPrompt", true) then
                table.insert(brainrots, obj)
            end
        end
    end
    
    local zonePlate = workspace:FindFirstChild("zone_plate")
    if zonePlate then
        local baseplate = zonePlate:FindFirstChild("Baseplate")
        if baseplate then
            local zoneBrainrotsFolder = baseplate:FindFirstChild("Brainrots")
            if zoneBrainrotsFolder then
                for _, obj in ipairs(zoneBrainrotsFolder:GetChildren()) do
                    if (obj:IsA("BasePart") or obj:IsA("Model")) and obj:FindFirstChildWhichIsA("ProximityPrompt", true) then
                        table.insert(brainrots, obj)
                    end
                end
            end
        end
    end
    
    return brainrots
end

-- Get object position
local function getObjectPosition(obj)
    if obj:IsA("BasePart") then
        return obj.Position + Vector3.new(0, 3, 0)
    elseif obj:IsA("Model") then
        local primaryPart = obj.PrimaryPart or obj:FindFirstChildWhichIsA("BasePart")
        if primaryPart then
            return primaryPart.Position + Vector3.new(0, 3, 0)
        end
    end
    return nil
end

-- Activate prompt (simple and working)
local function activatePrompt(brainrot)
    if not brainrot then return false end
    local prompt = brainrot:FindFirstChildWhichIsA("ProximityPrompt", true)
    if not prompt then debugLabel.Text = "No prompt in " .. brainrot.Name return false end
    
    prompt.Enabled = true
    prompt.HoldDuration = 0
    local success, err = pcall(function()
        fireproximityprompt(prompt, 0, false)
    end)
    if success then
        debugLabel.Text = "Activated: " .. brainrot.Name
        return true
    else
        debugLabel.Text = "Prompt error: " .. tostring(err)
        return false
    end
end

-- Function to destroy brainrot after stealing
local function destroyBrainrotAfterSteal(brainrotName)
    -- Check if the brainrot exists in workspace
    for _, obj in ipairs(workspace:GetChildren()) do
        if obj.Name == brainrotName and CheckIfItsABrainrot(obj.Name) then
            obj:Destroy()
            debugLabel.Text = "Destroyed stolen brainrot: " .. brainrotName
            break
        end
    end
end

-- Fast steal function
local function stealClosestBrainrot()
    if stealing then return end
    
    stealing = true
    
    local character = player.Character
    if not character or not character:FindFirstChild("HumanoidRootPart") then 
        debugLabel.Text = "No character"
        stealing = false
        return 
    end
    
    -- Unequip all tools before stealing
    unequipAllTools()
    
    -- Freeze player
    freezePlayer(true)
    
    local savedPos = character.HumanoidRootPart.Position
    debugLabel.Text = "Stealing..."
    statusLabel.Text = "Status: Stealing"
    stealButton.Text = "STEALING..."
    stealButton.BackgroundColor3 = Color3.fromRGB(244, 67, 54)
    
    -- Find brainrots
    local allBrainrots = findAllBrainrots()
    
    if #allBrainrots == 0 then
        debugLabel.Text = "Searching for brainrots..."
        if searchAroundBaseplate() then
            allBrainrots = findAllBrainrots()
        end
    end
    
    if #allBrainrots == 0 then
        debugLabel.Text = "No brainrots found"
        statusLabel.Text = "Status: No brainrots"
        stealButton.Text = "STEAL BRAINROT"
        stealButton.BackgroundColor3 = Color3.fromRGB(100, 150, 200)
        freezePlayer(false)
        stealing = false
        return
    end
    
    -- Sort by distance
    table.sort(allBrainrots, function(a, b)
        local posA = getObjectPosition(a)
        local posB = getObjectPosition(b)
        if not posA or not posB then return false end
        return (posA - savedPos).Magnitude < (posB - savedPos).Magnitude
    end)
    
    local closest = allBrainrots[1]
    local position = getObjectPosition(closest)
    if not position then 
        debugLabel.Text = "No valid position"
        stealButton.Text = "STEAL BRAINROT"
        stealButton.BackgroundColor3 = Color3.fromRGB(100, 150, 200)
        freezePlayer(false)
        stealing = false
        return 
    end
    
    debugLabel.Text = "Teleporting to: " .. closest.Name
    
    -- Teleport and collect
    if teleportTo(position) then
        task.wait(0.3)
        if activatePrompt(closest) then
            collectedCount += 1
            countLabel.Text = "Collected: " .. collectedCount
            
            -- Destroy the brainrot after stealing
            destroyBrainrotAfterSteal(closest.Name)
            
            task.wait(0.5)
            teleportToSell()
            task.wait(1.5)
            teleportTo(savedPos)
            debugLabel.Text = "Steal successful!"
            statusLabel.Text = "Status: Steal complete"
        else
            teleportTo(savedPos)
            debugLabel.Text = "Failed to activate"
            statusLabel.Text = "Status: Failed"
        end
    else
        debugLabel.Text = "Teleport failed"
        statusLabel.Text = "Status: Failed"
    end
    
    -- Reset
    stealButton.Text = "STEAL BRAINROT"
    stealButton.BackgroundColor3 = Color3.fromRGB(100, 150, 200)
    freezePlayer(false)
    stealing = false
    
    task.wait(1)
    if not collecting and not loopFarming then
        statusLabel.Text = "Status: Inactive"
        debugLabel.Text = "Ready"
    end
end

-- Main collection function
local function collectAllBrainrots()
    if not (collecting or loopFarming) then return end
    statusLabel.Text = "Status: Starting"
    
    while (collecting or loopFarming) do
        debugLabel.Text = "Looking for brainrots..."
        local allBrainrots = findAllBrainrots()
        
        if #allBrainrots == 0 then
            statusLabel.Text = "Status: No brainrots"
            local found = searchAroundBaseplate()
            if not found then
                if not loopFarming then break end
                task.wait(0.5)
                continue
            end
            allBrainrots = findAllBrainrots()
        end
        
        if #allBrainrots > 0 then
            statusLabel.Text = "Status: Found " .. #allBrainrots
            debugLabel.Text = "Processing"
            
            local character = player.Character
            if character and character:FindFirstChild("HumanoidRootPart") then
                local charPos = character.HumanoidRootPart.Position
                table.sort(allBrainrots, function(a, b)
                    local posA = getObjectPosition(a)
                    local posB = getObjectPosition(b)
                    if not posA or not posB then return false end
                    return (posA - charPos).Magnitude < (posB - charPos).Magnitude
                end)
            end
            
            for i, brainrot in ipairs(allBrainrots) do
                if not (collecting or loopFarming) then break end
                statusLabel.Text = "Status: Collecting " .. i .. "/" .. #allBrainrots
                debugLabel.Text = "Handling: " .. brainrot.Name
                local position = getObjectPosition(brainrot)
                if not position then continue end
                if not teleportTo(position) then continue end
                task.wait(0.4)
                if activatePrompt(brainrot) then
                    task.wait(0.6)
                    collectedCount += 1
                    countLabel.Text = "Collected: " .. collectedCount
                    
                    -- Destroy the brainrot after collecting
                    destroyBrainrotAfterSteal(brainrot.Name)
                    
                    teleportToSell()
                    task.wait(1.5)
                end
            end
        end
        
        if not loopFarming then break end
        searchAroundBaseplate()
    end
    
    collecting = false
    loopFarming = false
    toggleButton.Text = "START"
    toggleButton.BackgroundColor3 = Color3.fromRGB(76, 175, 80)
    originalToggleColor = toggleButton.BackgroundColor3
    loopButton.Text = "LOOP FARM"
    loopButton.BackgroundColor3 = Color3.fromRGB(200, 100, 50)
    originalLoopColor = loopButton.BackgroundColor3
    statusLabel.Text = "Status: Completed"
    debugLabel.Text = "Finished"
end

-- Toggle button
toggleButton.MouseButton1Click:Connect(function()
    collecting = not collecting
    if collecting then
        toggleButton.Text = "STOP"
        toggleButton.BackgroundColor3 = Color3.fromRGB(244, 67, 54)
        originalToggleColor = toggleButton.BackgroundColor3
        statusLabel.Text = "Status: Starting..."
        debugLabel.Text = "Initializing..."
        collectedCount = 0
        countLabel.Text = "Collected: 0"
        task.spawn(collectAllBrainrots)
    else
        toggleButton.Text = "START"
        toggleButton.BackgroundColor3 = Color3.fromRGB(76, 175, 80)
        originalToggleColor = toggleButton.BackgroundColor3
        statusLabel.Text = "Status: Inactive"
        debugLabel.Text = "Stopped"
        freezePlayer(false)
    end
end)

-- Loop button
loopButton.MouseButton1Click:Connect(function()
    loopFarming = not loopFarming
    if loopFarming then
        loopButton.Text = "STOP LOOP"
        loopButton.BackgroundColor3 = Color3.fromRGB(244, 67, 54)
        originalLoopColor = loopButton.BackgroundColor3
        statusLabel.Text = "Status: Starting Loop..."
        debugLabel.Text = "Initializing loop..."
        collectedCount = 0
        countLabel.Text = "Collected: 0"
        task.spawn(collectAllBrainrots)
    else
        loopButton.Text = "LOOP FARM"
        loopButton.BackgroundColor3 = Color3.fromRGB(200, 100, 50)
        originalLoopColor = loopButton.BackgroundColor3
        statusLabel.Text = "Status: Inactive"
        debugLabel.Text = "Loop stopped"
        freezePlayer(false)
    end
end)

-- Steal button
stealButton.MouseButton1Click:Connect(function()
    if collecting or loopFarming or stealing then 
        debugLabel.Text = "Wait for current operation"
        return 
    end
    task.spawn(stealClosestBrainrot)
end)

-- Shop button
shopButton.MouseButton1Click:Connect(function()
    local character = player.Character
    if not character or not character:FindFirstChild("HumanoidRootPart") then 
        debugLabel.Text = "No character"
        return 
    end
    
    local shopPos = getShopPosition()
    debugLabel.Text = "Going to shop..."
    statusLabel.Text = "Status: Going to shop"
    
    if teleportTo(shopPos) then
        debugLabel.Text = "Arrived at shop!"
        statusLabel.Text = "Status: At shop"
        task.wait(2)
        if not collecting and not loopFarming and not stealing then
            statusLabel.Text = "Status: Inactive"
            debugLabel.Text = "Ready"
        end
    else
        debugLabel.Text = "Failed to teleport to shop"
        statusLabel.Text = "Status: Shop teleport failed"
    end
end)

-- Auto-stop on reset
player.CharacterAdded:Connect(function()
    collecting = false
    loopFarming = false
    stealing = false
    freezePlayer(false)
    disableAntiVoid()
    toggleButton.Text = "START"
    toggleButton.BackgroundColor3 = Color3.fromRGB(76, 175, 80)
    loopButton.Text = "LOOP FARM"
    loopButton.BackgroundColor3 = Color3.fromRGB(200, 100, 50)
    statusLabel.Text = "Status: Auto-stopped"
    debugLabel.Text = "Auto-stopped (reset)"
    if screenGui and screenGui.Parent then
        screenGui:Destroy()
    end
end)

-- Enable anti-void on startup
enableAntiVoid()

-- Pre-cache
RunService.Heartbeat:Wait()
getSellPosition()