Exports

Exports

Sleepless Lootbox provides exports that allow other resources to interact with the lootbox system.

Server Exports

registerLootbox

Register a new lootbox at runtime from another resource.

exports.sleepless_lootbox:registerLootbox(name, data)

Parameters

ParameterTypeDescription
namestringUnique identifier for the lootbox (typically the item name)
datatableLootbox configuration data

Data Structure

PropertyTypeRequiredDescription
labelstringYesDisplay name shown in UI
descriptionstringNoDescription shown in UI
imagestringNoCustom image URL for the lootbox
registerItembooleanNoAuto-register as usable item
itemstableYesArray of loot items

Example

exports.sleepless_lootbox:registerLootbox('mystery_box', {
    label = 'Mystery Box',
    description = 'Who knows what\'s inside?',
    registerItem = true,
    items = {
        { 50, { name = 'bread', amount = 5 } },
        { 30, { name = 'water', amount = 3 } },
        { 15, { name = 'bandage', amount = 2 } },
        { 5, { name = 'medkit', amount = 1, rarity = 'rare' } },
    },
})

Wrap your registerLootbox call in a SetTimeout or wait for the resource to be fully loaded to ensure the export is available.


unregisterLootbox

Remove a previously registered lootbox.

exports.sleepless_lootbox:unregisterLootbox(name)

Parameters

ParameterTypeDescription
namestringThe lootbox identifier to remove

Example

exports.sleepless_lootbox:unregisterLootbox('mystery_box')

getLootbox

Get the configuration data for a specific lootbox.

local data = exports.sleepless_lootbox:getLootbox(name)

Parameters

ParameterTypeDescription
namestringThe lootbox identifier

Returns

TypeDescription
table|nilLootbox data or nil if not found

Example

local gunCase = exports.sleepless_lootbox:getLootbox('gun_case')
if gunCase then
    print('Found lootbox:', gunCase.label)
end

getAllLootboxes

Get all registered lootboxes.

local lootboxes = exports.sleepless_lootbox:getAllLootboxes()

Returns

TypeDescription
tableTable of all lootbox configurations keyed by name

Example

local allBoxes = exports.sleepless_lootbox:getAllLootboxes()
for name, data in pairs(allBoxes) do
    print(name, data.label)
end

open

Open a lootbox for a player, triggering the roll animation and giving the reward.

exports.sleepless_lootbox:open(source, caseName, skipItemRemoval)

Parameters

ParameterTypeDescription
sourcenumberPlayer server ID
caseNamestringThe lootbox identifier to open
skipItemRemovalbooleanIf true, the lootbox item won't be removed from inventory
⚠️

Set skipItemRemoval to true if you handle item removal yourself (e.g., via ox_target or custom triggers).

Example: Custom Trigger

RegisterNetEvent('myresource:openCase', function(caseName)
    local src = source
    -- Remove the item manually first
    exports.ox_inventory:RemoveItem(src, caseName, 1)
    -- Open with skipItemRemoval = true
    exports.sleepless_lootbox:open(src, caseName, true)
end)

Example: Standard Usage

-- Let sleepless_lootbox handle item removal
exports.sleepless_lootbox:open(source, 'gun_case', false)

getPreview

Get preview data for a lootbox, including all items with their calculated drop chances.

local preview = exports.sleepless_lootbox:getPreview(caseName)

Parameters

ParameterTypeDescription
caseNamestringThe lootbox identifier

Returns

TypeDescription
table|nilPreview data with items and chances, or nil if not found

Example

local preview = exports.sleepless_lootbox:getPreview('gun_case')
if preview then
    for _, item in ipairs(preview.items) do
        print(item.name, item.chance .. '%', item.rarity)
    end
end

Client Exports

preview

Request to show the preview modal for a lootbox, allowing players to view contents before opening.

exports.sleepless_lootbox:preview(caseName)

Parameters

ParameterTypeDescription
caseNamestringThe lootbox identifier to preview

Example

-- Show preview when player uses an item
RegisterNetEvent('myresource:previewCase', function(caseName)
    exports.sleepless_lootbox:preview(caseName)
end)

isRolling

Check if a lootbox roll animation is currently in progress.

local rolling = exports.sleepless_lootbox:isRolling()

Returns

TypeDescription
booleantrue if a roll is in progress, false otherwise

Example

if not exports.sleepless_lootbox:isRolling() then
    -- Safe to open another lootbox
    TriggerServerEvent('myresource:openCase', 'gun_case')
end

close

Close the lootbox UI.

exports.sleepless_lootbox:close()

Example

-- Force close the UI
exports.sleepless_lootbox:close()

Usage Examples

Integration with ox_target

-- Client side
exports.ox_target:addGlobalObject({
    {
        name = 'open_lootbox_crate',
        icon = 'fas fa-box-open',
        label = 'Open Crate',
        items = 'mystery_crate_item',
        onSelect = function(data)
            TriggerServerEvent('lootbox:openCrate')
        end,
    }
})
 
-- Server side
RegisterNetEvent('lootbox:openCrate', function()
    local src = source
    if exports.ox_inventory:GetItem(src, 'mystery_crate_item', false, true) then
        exports.ox_inventory:RemoveItem(src, 'mystery_crate_item', 1)
        exports.sleepless_lootbox:open(src, 'mystery_crate_item', true)
    end
end)

Dynamic Event Rewards

-- Server side: Give random lootbox as event reward
RegisterNetEvent('myevent:claimReward', function()
    local src = source
    local lootboxes = { 'gun_case', 'supply_crate', 'vip_case' }
    local randomBox = lootboxes[math.random(#lootboxes)]
    
    -- Open directly without requiring the item
    exports.sleepless_lootbox:open(src, randomBox, true)
end)

Preview Before Purchase

-- Client side: Show preview in a shop
RegisterNetEvent('shop:previewLootbox', function(caseName)
    exports.sleepless_lootbox:preview(caseName)
end)