> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jaksam-scripts.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Register metadata template

> Adds a metadata template from an external script, used by items with dynamic metadata.

Adds a metadata template from an external script. A metadata template is a function that generates a metadata value every time an item that uses it is added to an inventory (a serial number, the owner's name, a random stat, etc.).

Templates registered this way work exactly like the ones in `metadata_templates.lua`: they appear in the item editor of the `/inventory` admin menu, where you can assign them to any item. See the [Metadata guide](/jaksam-inventory/guides/metadata) for how templates are assigned to items.

<Note>
  Templates are kept in memory only. They are removed when your script stops and lost when jaksam\_inventory restarts, so register them every time your script starts.
</Note>

<CodeGroup>
  ```lua Export theme={null}
  exports['jaksam_inventory']:registerMetadataTemplate(templateId, cb)
  ```

  ```lua Example theme={null}
  local function registerTemplates()
      exports['jaksam_inventory']:registerMetadataTemplate('my_serial', function(playerId, inventoryId, itemName, amount, metadata, slotId)
          return 'SN-' .. math.random(100000, 999999)
      end)
  end

  -- Register on start, and again if jaksam_inventory is restarted after your script
  registerTemplates()

  AddEventHandler('onResourceStart', function(resourceName)
      if resourceName == 'jaksam_inventory' then registerTemplates() end
  end)
  ```
</CodeGroup>

### Parameters

| Name | Data Type | Description |
| - | - | - |
| `templateId` | string | The template ID, the one you pick in the item editor |
| `cb` | function | Called every time an item using the template is added. Its return value becomes the metadata value |

#### Callback parameters

| Name | Data Type | Description |
| - | - | - |
| `playerId` | number \| nil | Server ID of the player, only when the item is added to a player inventory |
| `inventoryId` | string | The inventory the item is added to |
| `itemName` | string | The item being added |
| `amount` | number | The amount being added |
| `metadata` | table \| nil | The metadata the item is being added with |
| `slotId` | string \| nil | The slot the item is being added to, if one was specified |

### Return value

None

### Notes

* Server side only, as metadata is always generated by the server
* If your template ID is the same as one in `metadata_templates.lua`, yours replaces it while your script is running, and the original one comes back when your script stops
* While a template is not registered (your script hasn't started yet, or has stopped), items using it are still added normally, only without that metadata field, and the server console shows a warning


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.