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
| Parameter | Type | Description |
|---|---|---|
name | string | Unique identifier for the lootbox (typically the item name) |
data | table | Lootbox configuration data |
Data Structure
| Property | Type | Required | Description |
|---|---|---|---|
label | string | Yes | Display name shown in UI |
description | string | No | Description shown in UI |
image | string | No | Custom image URL for the lootbox |
registerItem | boolean | No | Auto-register as usable item |
items | table | Yes | Array 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
| Parameter | Type | Description |
|---|---|---|
name | string | The 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
| Parameter | Type | Description |
|---|---|---|
name | string | The lootbox identifier |
Returns
| Type | Description |
|---|---|
table|nil | Lootbox data or nil if not found |
Example
local gunCase = exports.sleepless_lootbox:getLootbox('gun_case')
if gunCase then
print('Found lootbox:', gunCase.label)
endgetAllLootboxes
Get all registered lootboxes.
local lootboxes = exports.sleepless_lootbox:getAllLootboxes()Returns
| Type | Description |
|---|---|
table | Table 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)
endopen
Open a lootbox for a player, triggering the roll animation and giving the reward.
exports.sleepless_lootbox:open(source, caseName, skipItemRemoval)Parameters
| Parameter | Type | Description |
|---|---|---|
source | number | Player server ID |
caseName | string | The lootbox identifier to open |
skipItemRemoval | boolean | If 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
| Parameter | Type | Description |
|---|---|---|
caseName | string | The lootbox identifier |
Returns
| Type | Description |
|---|---|
table|nil | Preview 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
endClient Exports
preview
Request to show the preview modal for a lootbox, allowing players to view contents before opening.
exports.sleepless_lootbox:preview(caseName)Parameters
| Parameter | Type | Description |
|---|---|---|
caseName | string | The 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
| Type | Description |
|---|---|
boolean | true 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')
endclose
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)