Document
An abstraction over keys in a DataStore.
Documents are designed to contain information about an entity in a schema. This schema is enforced by your check function, and should be changed through migrations. You may, of course, decide to not use a schema by defining an empty check function, but this generally isn't recommended.
tip
Session locking prevents your data from being edited by mutliple servers, and ensures one server is finished with it before it is opened by another.
In DocumentService, session locking enables the use of the caching methods
SetCache
and GetCache
.
This is ideal for player data, or any data that needs frequent updates and does not need multi-server editing.
warning
You are free to edit the contents of the table in the .data field with a tool like DataStore Editor, but manually changing other fields could cause data loss and errors.
warning
Types for your data are provided under the assumption that once a document is opened, the underlying data held in Data Stores is not updated externally in a way that changes its type.
Types
OpenResult<T>
type
OpenResult<T> =
Result
<
T,
RobloxAPIError
|
BackwardsCompatibilityError
|
CheckError
|
SessionLockedError
>
WriteResult<T>
Functions
Open
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsValidates the document if one exists, creates a default document if no document exists, or creates a document with the data that is in the given key if the key hasn't been used with DocumentService before.
A document needs to be opened before use (even if it is not session-locked),
as this ensures all migrations run and the cases of missing or legacy data
are handled. If you only need to update a document once, consider
OpenAndUpdate
.
Opening a session-locked document will enable periodic autosaves until it is closed.
You must open a document before reading or writing to it.
info
If the document is locked by another session, this method will wait and retry up to 5 times, and yields until the retries are exhausted or the lock is removed. Therefore, you should not use this method to check if the Document is being used by another session.
warning
You should check the value of success
, and handle failures by checking
the value of reason
. The possible reason
s for each method are defined in
the return type.
OpenAndUpdate
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsOpens, and also runs a transform function on the data. Useful for non-session-locked data for shared entities, where one-off updates might be needed.
Will throw a Luau error if the transform produces invalid or unsavable data.
Runs both Open and Update hooks and signals, including fail hooks.
Steal
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Marks the lock as stolen. The next :Open
call will ignore any existing
locks.
info
Generally, it is recommended to call :Steal
and then :Open
in the case
that the initial :Open
fails due to SessionLockedError
.
warning
Do not use this unless you are very sure the previous session is dead, or you could cause data loss. Only usable on session-locked Documents.
GetOpenedSignal
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Retrieves a signal that is fired when the document is opened. Note that this will
be fired on completion of any :Open
, even if it failed.
GetClosedSignal
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Retrieves a signal that is fired when the document is closed. Note that this will
be fired on completion of any :Close
, even if it failed.
GetUpdatedSignal
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Retrieves a signal that is fired when the document is updated.
For example, this will be fired after an autosave or after you call :Save()
.
Note that this will be fired on completion of any :Update
, even if it failed.
GetReadSignal
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Retrieves a signal that is fired when the document is read. Note that this will
be fired on completion of any :Read
, even if it failed.
GetCacheChangedSignal
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsRetrieves a signal that is fired when the document's cache is set.
IsOpenAvailable
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsReturns a false Result if Document is either: i. Currently open in this server ii. Currently locked by another session
Otherwise returns a true Result.
If props.lockSessions is false, this will always return a true Result.
tip
You can use this to check if a player is active to avoid data loss while editing data from another server.
IsOpen
Returns whether the document is open or not
Close
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsCloses the document, so it cannot be edited.
The document must be open before using this method.
If session locked, will save the document, remove the lock, and cancel autosaves first. If this fails, the document will not be closed.
If the Close performs a Save, Result.data will be the data saved.
IsClosing
Returns true if :Close
has been called and is incomplete.
SetCache
Sets the cache.
The document must be open before using this method. You can only use cache for session-locked data.
warning
Never yield between a GetCache and a SetCache while modifying data or you may revert data.
warning
Your cache should always pass your check function, otherwise autosaves may error.
info
You must use immutable operations on cache, i.e. clone any table you intend to edit.
GetCache
Retrieves the cache.
The document must be open before using this method. You can only use cache for session-locked data.
info
You must use immutable operations on cache, i.e. clone any table you intend to edit.
Update
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsDocument.
Update
(
transform:
Transform
<
T
>
--
Transform function for the transaction.
) →
WriteResult
<
T
>
Performs an atomic transaction on the Document, writing to the DataStore.
The document must be open before using this method.
If using session locking, passing a transform is equvialent to calling
SetCache
with the transformed data and then Save
.
If the Save fails, the cache will still be updated.
If you only need to update a document once, consider OpenAndUpdate
.
Throws if data is not storable or the transform return value is invalid.
tip
Due to Luau limitations with the old solver, you will get the best experience if you manually annotate the type of the transform parameter.
warning
The transform function must not yield, and shouldn't rely on any data from outside. It must follow the rules of what is storable in Data Stores.
warning
Assumes the data that is already in Data Stores is valid since the last
:Open
. If it isn't, and this is not corrected by the transform, this
method will throw a luau error.
warning
If you are using session locking, your transform needs to use immutable operations (in the same way updating cache does).
warning
If your transform errors, the update will be aborted and the error will be thrown in a new thread (this is Roblox behaviour).
info
Unlike Open
, this method will not retry if the lock is stolen, and will
instead return a SessionLockedError
after the first attempt.
Save
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsSaves a Document's cache to its DataStore. Equivalent to calling Update without transforming the data.
The document must be open and locked to use this method.
Returns a WriteResult with the data field being the data saved to Data Stores.
Erase
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsErases all data associated with the key.
The document must not be open. It is up to you to check if the document
is open elsewhere, e.g. via IsOpenAvailable
.
Satisfies compliance with GDPR right of erasure.
Does not run hooks.
Read
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsDocument.
Read
(
self:
Document
<
T
>
) →
Result
<
any,
RobloxAPIError
|
SchemaError
|
CheckError
|
BackwardsCompatibilityError
>
Reads the latest data stored in Data Stores.
Runs migrations and the check function, but does not save changes.
This may be called while the document is not open.
warning
A SchemaError
will be returned if document has never been opened before,
so it is strongly recommended to handle this case, and Open the document
before reading it if possible. This includes when migrating from no library.
Runs Read hooks.
HookBefore
Document.
HookBefore
(
hook:
(
)
→
(
)
--
a hook function that receives the arguments passed in to the operation
) →
cleanup
--
a callback that removes the hook from the given hook event registry
Attaches a hook which occurs before the event.
Note that if a hook yields, it will yield all methods that call it. Hooks are called in the order they are added.
Hooks cannot currently mutate arguments.
OnceBefore
Document.
OnceBefore
(
hook:
(
)
→
(
)
--
a hook function that receives the arguments passed in to the operation
) →
(
)
Attaches a single-use hook which occurs before the event.
isDocument
Document.
isDocument
(
instance:
metatable
) →
boolean
Checks if a metatable passed is a Document.
HookAfter
This was deprecated in v1.2.0
use signals
Document.
HookAfter
(
hook:
(
)
→
(
)
--
a hook function that receives the arguments passed in to the operation
) →
cleanup
--
a callback that removes the hook from the given hook event registry
Attaches a hook which occurs after the event, before the method returns.
Note that if a hook yields, it will yield all methods that call it. Hooks are called in the order they are added.
Hooks added with HookAfter only run if the operation is successful, and cannot mutate the result.
HookFail
This was deprecated in v1.2.0
use signals
Document.
HookFail
(
hook:
(
)
→
(
)
--
a hook function that receives the arguments passed in to the operation
) →
cleanup
--
a callback that removes the hook from the given hook event registry
Attaches a hook which occurs after an event fails.
Note that fail hooks only run when a method returns an Err type. They will not run if the method throws a Luau error due to incorrect usage.
OnceAfter
This was deprecated in v1.2.0
use signals
Document.
OnceAfter
(
hook:
(
)
→
(
)
--
a hook function that receives the arguments passed in to the operation
) →
(
)
Attaches a single-use hook which occurs after the event, before the method returns.
OnceFail
This was deprecated in v1.2.0
use signals
Document.
OnceFail
(
hook:
(
)
→
(
)
--
a hook function that receives the arguments passed in to the operation
) →
(
)
Attaches a single-use hook which occurs after an event fails.