Store
Stores a collection of data.
Types
StoreOptions<TKey, TData>
interface
StoreOptions<TKey, TData> {
dataStore:
GlobalDataStore?
--
The GlobalDataStore to use, defaults to the data store with the name of the store
lockId:
string?
--
The ID to use for locking, this should be unique per server, defaults to Store.defaultLockId
key:
(
(
key:
TKey
)
→
string
)
?
--
A function that takes a key and returns a string key for the data store, this is not needed if the key is a string
data:
(
key:
TKey
)
→
TData
--
A function that takes a key and returns the data to store in the data store
default:
(
key:
TKey
)
→
TData
--
A function that takes a key and returns the default data
userIds:
(
(
key:
TKey
)
→
{
number
}
?
)
?
--
A function that takes a key and returns an array of user ids associated with the key
releaseSessionsOnClose:
boolean?
--
Whether or not to release all sessions when the server closes, defaults to true
autosaveSeconds:
number?
--
How many seconds between autosave cycles, set to -1 to disable autosaves, defaults to 30
}
The options for the Store
Properties
defaultLockId
staticStore.defaultLockId:
string
The default Store.lockId.
This defaults to a random GUID such as ff97f92b48a5472d96463ecf64c32866
.
datastore
Store.datastore:
GlobalDataStore
The underlying data store used.
lockId
Store.lockId:
string
A string that should be unique per server, this will be used for session locking. This defaults to Store.defaultLockId.
name
Store.name:
string
The name of the store.
sessions
The sessions that are loaded. The key is the string key of the session.
sessionReleased
SignalFired whenever a session in the store has been released.
The Session that released is given as first parameter. The second parameter is a boolean containing whether the data was saved or not (see Session.released).
store.sessionReleased:Connect(function(session, didSave)
print(`Session "{session.keyStr}" has been released! didSave: {didSave}`)
end)
Functions
new
Store.
new
(
name:
string
,
--
The name of the store
options:
StoreOptions
<
TKey,
TData
>
--
Options for the store
) →
Store
<
TKey,
TData
>
Creates a store with the given name.
getKey
Store:
getKey
(
key:
TKey
) →
string
Gets the string key used in the datastore.
getData
Store:
getData
(
key:
TKey
) →
TData
Gets the data to store in the datastore.
getDefault
Store:
getDefault
(
key:
TKey
) →
TData
Gets the default data to store in the datastore.
getMetadata
Store:
getMetadata
(
key:
TKey
) →
{
[
any
]
:
any
}
?
Gets the metadata to store in the datastore.
getUserIds
Store:
getUserIds
(
key:
TKey
) →
{
number
}
?
Gets the user ids to store in the datastore.
getSession
Gets an existing session using the key.
load
Store:
load
(
key:
TKey
,
--
The key to load
onSessionLocked:
(
"requestRelease"
|
"steal"
)
?
,
--
What to do if the session is locked
default:
TData?
--
The default value to use, overwrites [StoreOptions.default]
) →
Promise
<
Session
<
TData,
TKey
>
>
Attempts to load the session with the given key.
The onSessionLocked
parameter specifies what to do if the session is locked:
-
"requestRelease"
: This will repeatedly try to load the session and also tells the server that locked the session to release the session, saving all the data and removing the lock. -
"steal"
: This will steal the lock, overwriting the existing lock.
Data Loss
Using "steal"
might cause data loss because the other server has no chance to
save the data!