Untitled

Anonymous
plain_text
02/11/2026 2:07 PM
1.6 KB
10
Indexable
function ObjectPool.CreateInstancePool(
    template: Instance,
    minSize: number?,              -- nilable
    maxSize: number?,              -- nilable
    autoExpand: boolean?,          -- nilable
    growthStep: number?,           -- nilable
    opts: {
        parent: Instance?,
        reset: ((Instance) -> ())?,
        destroy: ((Instance) -> ())?,
    }?
): ObjectPool<Instance>
    assert(typeof(template) == "Instance", "CreateInstancePool: 'template' must be an Instance")

    opts = opts or {}

    local parent = opts.parent or template.Parent

    -- wrap user reset so parent is always correct
    local userReset = opts.reset
    local function wrappedReset(obj: Instance)
        obj.Parent = parent
        if userReset then
            userReset(obj)
        end
    end

    return ObjectPool.new({
        factory = function()
            local clone = template:Clone()
            clone.Parent = parent
            return clone
        end,
        reset = wrappedReset,
        destroy = opts.destroy,
        minSize = minSize,
        maxSize = maxSize,
        autoExpand = if autoExpand == nil then true else autoExpand,
        growthStep = growthStep,
    })
end
Editor is loading...
Leave a Comment