CCCPaste
-- Tower Chest Collection - Auto-collects chests by teleporting them to player
-- Extracted from Tower Autofarm.lua

local Players = game:GetService('Players')
local Workspace = game:GetService('Workspace')
local RunService = game:GetService('RunService')

-- Configuration via getgenv
local _genv = getgenv()
if _genv.TowerChestEnabled == nil then
    _genv.TowerChestEnabled = true
end

-- Custom wait function using Heartbeat
local function wait(sec)
    sec = tonumber(sec)
    if sec and sec > 0 then
        local t0 = os.clock()
        while os.clock() - t0 < sec do
            RunService.Heartbeat:Wait()
        end
    else
        RunService.Heartbeat:Wait()
    end
end

-- Get player
local plr = Players.LocalPlayer
if not plr then
    warn('[TowerChest] Player not found')
    return
end

-- Get HumanoidRootPart
local function getHRP()
    if not plr.Character then
        return nil
    end
    return plr.Character:FindFirstChild('HumanoidRootPart')
end

print('[TowerChest] Initialized. Use getgenv().TowerChestEnabled = false to disable')

-- Main chest collection loop
spawn(function()
    while true do
        wait(1)
        
        if not _genv.TowerChestEnabled then
            wait(1)
        else
            pcall(function()
                -- Get all objects in workspace
                for _, v in ipairs(Workspace:GetChildren()) do
                    -- Check if it's a chest model
                    if v and v:IsA('Model') and string.find(v.Name:lower(), 'chest') then
                        pcall(function()
                            local hrp = getHRP()
                            local primaryPart = v.PrimaryPart
                            
                            if hrp and primaryPart then
                                -- Teleport chest to player to trigger collection
                                primaryPart.CFrame = hrp.CFrame
                            end
                        end)
                    end
                end
            end)
        end
    end
end)

-- Listen for new chests as they spawn (optional, for faster collection)
Workspace.ChildAdded:Connect(function(v)
    if not _genv.TowerChestEnabled then return end
    
    if v and v:IsA('Model') and string.find(v.Name:lower(), 'chest') then
        spawn(function()
            task.wait(0.1)
            
            pcall(function()
                local hrp = getHRP()
                local primaryPart = v.PrimaryPart
                
                if hrp and primaryPart then
                    primaryPart.CFrame = hrp.CFrame
                end
            end)
        end)
    end
end)

-- API for control
local ChestAPI = {}

function ChestAPI.enable()
    _genv.TowerChestEnabled = true
end

function ChestAPI.disable()
    _genv.TowerChestEnabled = false
end

function ChestAPI.toggle()
    _genv.TowerChestEnabled = not _genv.TowerChestEnabled
end

-- Manually collect all visible chests right now
function ChestAPI.collectAll()
    pcall(function()
        local hrp = getHRP()
        if not hrp then return end
        
        local count = 0
        for _, v in ipairs(Workspace:GetChildren()) do
            if v and v:IsA('Model') and string.find(v.Name:lower(), 'chest') then
                pcall(function()
                    if v.PrimaryPart then
                        v.PrimaryPart.CFrame = hrp.CFrame
                        count = count + 1
                    end
                end)
            end
        end
    end)
end

_G.ChestAPI = ChestAPI

-- Create GUI
local function createGUI()
    local plr = Players.LocalPlayer
    if not plr then return end
    
    local playerGui = plr:FindFirstChild('PlayerGui')
    if not playerGui then return end
    
    -- Create ScreenGui
    local screenGui = Instance.new('ScreenGui')
    screenGui.Name = 'TowerChestGui'
    screenGui.ResetOnSpawn = false
    screenGui.Parent = playerGui
    
    -- Create main frame
    local mainFrame = Instance.new('Frame')
    mainFrame.Name = 'MainFrame'
    mainFrame.Size = UDim2.new(0, 150, 0, 80)
    mainFrame.Position = UDim2.new(0, 10, 0, 10)
    mainFrame.BackgroundColor3 = Color3.new(0.1, 0.1, 0.1)
    mainFrame.BorderSizePixel = 1
    mainFrame.BorderColor3 = Color3.new(0.4, 0.4, 0.4)
    mainFrame.Parent = screenGui
    
    -- Create title
    local titleLabel = Instance.new('TextLabel')
    titleLabel.Name = 'Title'
    titleLabel.Size = UDim2.new(1, 0, 0, 30)
    titleLabel.Position = UDim2.new(0, 0, 0, 0)
    titleLabel.BackgroundColor3 = Color3.new(0.08, 0.08, 0.08)
    titleLabel.BorderSizePixel = 0
    titleLabel.TextColor3 = Color3.new(1, 0.85, 0)
    titleLabel.TextSize = 14
    titleLabel.Text = 'Tower Chest'
    titleLabel.Parent = mainFrame
    
    -- Create toggle button
    local toggleButton = Instance.new('TextButton')
    toggleButton.Name = 'ToggleButton'
    toggleButton.Size = UDim2.new(1, -10, 0, 40)
    toggleButton.Position = UDim2.new(0, 5, 0, 35)
    toggleButton.BackgroundColor3 = Color3.new(0, 0.6, 0)
    toggleButton.BorderSizePixel = 1
    toggleButton.BorderColor3 = Color3.new(0.4, 0.4, 0.4)
    toggleButton.TextColor3 = Color3.new(1, 1, 1)
    toggleButton.TextSize = 12
    toggleButton.Text = 'Disable'
    toggleButton.Parent = mainFrame
    
    -- Button click handler
    toggleButton.MouseButton1Click:Connect(function()
        ChestAPI.toggle()
        if _genv.TowerChestEnabled then
            toggleButton.Text = 'Disable'
            toggleButton.BackgroundColor3 = Color3.new(0, 0.6, 0)
            titleLabel.TextColor3 = Color3.new(0, 1, 0)
        else
            toggleButton.Text = 'Enable'
            toggleButton.BackgroundColor3 = Color3.new(0.6, 0, 0)
            titleLabel.TextColor3 = Color3.new(1, 0.2, 0.2)
        end
    end)
end

-- Wait for player to be ready, then create GUI
spawn(function()
    task.wait(1)
    pcall(createGUI)
end)

return ChestAPI