> For the complete documentation index, see [llms.txt](https://ans-dev.gitbook.io/datareplicator/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ans-dev.gitbook.io/datareplicator/api-reference/server-side-api/registerkey.md).

# :RegisterKey

Pre-registers a data key and its options on the server, essentially "whitelisting" it as a valid key before any data is created for it.

This is a crucial utility for preventing race conditions and enhancing security. It's highly recommended to register all your data keys when the server starts. Client requests for keys that have not been registered are considered suspicious and will be penalized by the intelligent rate limiting system.

**When to Use** [**`:RegisterKey`**](/datareplicator/api-reference/server-side-api/registerkey.md) **vs.** [**`:Create`**](/datareplicator/api-reference/server-side-api/create.md)**?**

* Use [`:RegisterKey()`](/datareplicator/api-reference/server-side-api/registerkey.md) when your server starts to define all possible data keys your game will use. This is perfect for keys where data might be loaded asynchronously (e.g., from a [DataStore](https://create.roblox.com/docs/cloud-services/data-stores)) and won't be available immediately.
* Use [`:Create()`](/datareplicator/api-reference/server-side-api/create.md) when you are ready to set the initial data for a key. If the key wasn't previously registered, [`:Create()`](/datareplicator/api-reference/server-side-api/create.md) will also register it for you.

***

**`DataReplicator:RegisterKey(realKey: string, options: table?)`**

***

{% tabs %}
{% tab title="Parameters" %}

* `realKey`: [string](https://create.roblox.com/docs/luau/strings)

  The unique, case-sensitive key for the data. It's a good practice to use a consistent naming convention (e.g., "`PlayerData_12345`").
* `options`: [table?](https://create.roblox.com/docs/luau/tables)

  An optional dictionary to configure advanced features for this data key. These settings are locked in the first time the key is registered or created.

  * `Encrypted`: [boolean](https://create.roblox.com/docs/luau/booleans) - (Default: `false`) If `true`, all data for this key will be protected by transport encryption. A unique session key is securely established for each player using a post-quantum key exchange. Best for sensitive information like currency or private settings.
  * `Priority`: [string](https://create.roblox.com/docs/luau/strings) - (Default: `"Medium"`) Sets the update priority. Can be `"High"`, `"Medium"`, or `"Low"`.
  * `UseDeltaCompression`: [boolean](https://create.roblox.com/docs/luau/booleans) - (Default: `false`) If `true`, only changes within a table will be replicated after the initial full sync.
    {% endtab %}

{% tab title="Returns" %}
[`boolean`](https://create.roblox.com/docs/luau/booleans) - `true` if the key was successfully registered.
{% endtab %}

{% tab title="Example" %}
{% code title="RegisterKeyTest.luau" %}

```lua
-- In a server script that manages player sessions
local Players = game:GetService("Players")

local function onPlayerAdded(player)
	local userId = player.UserId
	
	-- Pre-register all expected keys for this player immediately.
	-- This prevents the client from being penalized for requesting data that is still loading.
	
	-- Registering player stats with high priority and delta compression
	DataReplicator:RegisterKey(`PlayerData_{userId}`, {
		Priority = "High",
		UseDeltaCompression = true
	})
	
	-- Registering a sensitive key that will be encrypted
	DataReplicator:RegisterKey(`PlayerSettings_{userId}`, {
		Encrypted = true
	})
	
	-- Now, load the data asynchronously from a DataStore
	task.spawn(function()
		local success, data = pcall(function()
			-- local playerData = myDataStore:GetAsync(userId)
			-- return playerData
			return { Coins = 100, Level = 5, Equipment = {} } -- Example data
		end)
		
		if success and data then
			-- Now that data is loaded, create it in the replicator.
			-- The options are already set from RegisterKey, so we don't need to pass them again.
			DataReplicator:Create(`PlayerData_{userId}`, data)
			print(`Initial data created for player {userId}`)
		else
			warn(`Failed to load data for player {userId}`)
		end
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
```

{% endcode %}
{% endtab %}
{% endtabs %}
