: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 vs. :Create?
Use
:RegisterKey()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) and won't be available immediately.
DataReplicator:RegisterKey(realKey: string, options: table?)
realKey: stringThe unique, case-sensitive key for the data. It's a good practice to use a consistent naming convention (e.g., "
PlayerData_12345").options: table?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 - (Default:false) Iftrue, 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 - (Default:"Medium") Sets the update priority. Can be"High","Medium", or"Low".UseDeltaCompression: boolean - (Default:false) Iftrue, only changes within a table will be replicated after the initial full sync.
boolean - true if the key was successfully registered.
-- 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)Last updated