Storage
Storage is an easy to use datastore solution that handles all the data loading and saving hassle for you while remaining simple to understand and apply.
Variables
SetCache: table used to cache set requestsGetCache: table used to cache get requestsMessagingCache: table used to cache cross-server dataMessagingLastSend: internal number used to prevent quick-join/leave throttlingStores: table used to cache datastoresEventPipeline: table used to cache object value change connections
Functions
Event(Key | string/Player, Callback | function)
Sets up the specified Callback to run whenever the object of the specified key's value changes.
Storage:Event(Player, function(Value)
print("New value for ".. Player.Name.. ":", Value)
end)
Value(Object | string) [deprecated]
Returns the actual value from the specified string.
print(type("1")) -- string
print(type(Storage:Value("1"))) -- number
DeserializeTable(Table | table)
Returns the table in a deserialized format, turning compressed data into its actual types.
Deserialize(String | string)
Returns the JSON string in a deserialized table format, turning compressed data into its actual types.
SerializeTable(Table | table)
Returns the table in a serialized format, compressing data.
Serialize(Table | table)
Returns a JSON string of the table in a serialized format, compressing data.
Object(Table | table, Key | string/Player)
Returns the object stored by the specified key of the specified table.
TableRaw(Key | string, Ordered | bool)
Returns the table stored by the specified key.
Table(Key | string, Ordered | bool)
Returns the table stored by the format tostring(game.GameId).. "|".. Key.
Using Storage
Basic Understanding
Storage was designed to be simple. As such, scripting data saving and loading is very easy using this library.
In Storage, there are two main data groups: Tables and Objects.
Tables are the highest class, holding Objects inside them.
Objects are holders of data. An Object can be any Lua data type, up to a table. Each Object is stored by a key.
Here's an example of basic data that persists:
local Data = Storage:Table("Data")
Data["Message"] = "Test 123"
To do this, you can use
Table:Get(Key).
Once you have obtained your Object, if it is a table, you can define its data.
Some of the magic of Storage comes from the fact that if you save an Object as a table, you can change its contents at any time and those changes will save properly.
local Data = Storage:Table("Data")
Data["Message"] = { Content = "Test 123" }
local Message = Data:Get("Message")
print(Message.Content) -- "Test 123"
Message.Content = "Test 2"
print(Message.Content) -- "Test 2"
Message = Data:Get("Message")
print(Message.Content) -- "Test 2"
Ordered Use Case
When using an ordered table, you will only be able to store objects as numbers.
In order to make sorting through tables easier for systems such as leaderboards, you can use the Table:Sort(Ascending | bool: false, Amount | number: 50, Min | number, Max | number) function.
local Points = Storage:Table("Points", true)
local SortedPoints = Points:Sort()
for i, v in pairs(SortedPoints) do
local Order = i
local Key = v.key -- If the Key is a player, you can use Key:match("%d+") to retrieve the user id
local Value = v.value
end
Removing Data
You might run into situations where you may want to remove data stored using Storage. For example, in order to comply with GDPR requests you will need to remove stored data. As such, you must use the following format if you use the Table function:
-- Non-Ordered
Delta.DataStore:GetDataStore(tostring(game.GameId).. "|Key"):RemoveAsync("player_".. tostring(UserId)) -- If removing a player
Delta.DataStore:GetDataStore(tostring(game.GameId).. "|Key"):RemoveAsync("key") -- If removing a key
-- Ordered
Delta.DataStore:GetOrderedDataStore(tostring(game.GameId).. "|Key"):RemoveAsync("player_".. tostring(UserId)) -- If removing a player
Delta.DataStore:GetOrderedDataStore(tostring(game.GameId).. "|Key"):RemoveAsync("key") -- If removing a key