# Temporary Inventories

Temporary Inventories are Inventories that are loaded during runtime when needed, and unloaded when no player is accessing the Inventory. For example this can be used for stashes.\ <sub>***In this example we are going to make a police storage.***</sub>

{% hint style="warning" %}
In order to use this, you need to have *some* experience with coding.
{% endhint %}

***

First we need to register a temporary inventory handler using [RegisterTemporaryInventoryHandler](/bluebyte/scripts/advanced-inventory/api/server-exports/registertemporaryinventoryhandler.md).\
Temporary inventory handler are the code that is called when the police locker is accessed, in this code you can add checks & load the specific inventory that is needed.

```lua
local api = exports["bb_inventory"]:GetAPI()

api.RegisterTemporaryInventoryHandler("police_storage",
    function(identifier, temporaryData, source)
        -- Add any checks you'd like,
        -- such as checking if the player has the right to access this storage
    end)
```

***

After registering the temporary inventory handler, we can load the inventory by the identifier(If it exists) using [LoadInventoryByIdentifier](/bluebyte/scripts/advanced-inventory/api/server-exports/loadinventorybyidentifier.md).

```lua
local api = exports["bb_inventory"]:GetAPI()

api.RegisterTemporaryInventoryHandler("police_storage",
    function(identifier, temporaryData, source)
        -- Add any checks you'd like,
        -- such as checking if the player has the right to access this storage

        local inventory = api.LoadInventoryByIdentifier(identifier)
        if (inventory) then
            return inventory
        end
    end)
```

***

Now, if the inventory does not exist, we create a new one using [CreateInventory](/bluebyte/scripts/advanced-inventory/api/server-exports/createinventory.md).

{% hint style="warning" %}
The third parameter of CreateInventory is 'container', make sure the container is registered in your configs/container.shared.lua.\
\
For example:

```lua
["police_storage"] = {
    sizeX = 6,
    sizeY = 6,
    maxWeight = 40000 -- Also can be null
}
```

{% endhint %}

```lua
local api = exports["bb_inventory"]:GetAPI()

api.RegisterTemporaryInventoryHandler("police_storage",
    function(identifier, temporaryData, source)
        -- Add any checks you'd like,
        -- such as checking if the player has the right to access this storage

        local inventory = api.LoadInventoryByIdentifier(identifier)
        if (inventory) then
            return inventory
        end

        -- If the inventory does not exist, we create a new one
        return api.CreateInventory(identifier, "STASH", "police_storage", "police_storage", "police_storage",
            true)
    end)
```

***

Now, all the server-side part is done. We are now going to access the inventory on the client-side.\
Thats quite easy, we only need to make one API call, thats [AccessInventory](/bluebyte/scripts/advanced-inventory/api/client-exports/accessinventory.md).

&#x20;

```lua
local api = exports["bb_inventory"]:GetAPI()

-- Everything in front of the colon is the 'identifierHandle',
-- which is the one that is needed at RegisterTemporaryInventoryHandler

-- The second parameter is the temporaryData, which can be nil if you don't need it
-- It can be used to pass any data you want to the inventory handler
-- The third and fourth parameters are the title and icon of the inventory,
-- which are temporary until the title & icon is fetched from the database
-- which can also be nil if you don't need them
api.AccessInventory("police_storage:5", nil, "police_storage", "police_storage")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bluebyte.dev/bluebyte/scripts/advanced-inventory/temporary-inventories.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
