Files
Handles the storage and retrieval of potentially large data blobs within Roblox DataStores,
working around the size limitations imposed by DataStore SetAsync/GetAsync calls.
Core Problem: Roblox DataStores have a maximum size limit per key (currently 4MB). Data that exceeds this limit cannot be stored directly.
Solution: Sharding & Compression
-
Sharding: If the JSON-encoded data exceeds the configured
maxShardSize, it is split into multiple smaller chunks (shards). Metadata about these shards (a unique shard ID and the total shard count) is stored in the primary DataStore entry for the original key, while the actual shard data is stored under separate keys derived from the shard ID. -
Compression: Before sharding, the JSON-encoded data is converted to binary using
buffer.fromstring. This binary representation is then JSON-encoded again before being split into shards. Roblox automatically compresses buffers when encoding them usingJSONEncode. This helps reduce the number of shards required, minimizing DataStore requests.
Shard Key Naming: Shard data is stored using keys formatted as {shardId}-{shardIndex},
where shardId is a unique GUID generated for the file and shardIndex is the 1-based
index of the shard.
Types
WriteParams
interface WriteParams {data: any--
The Luau data to be stored. Must be JSON-encodable.
maxShardSize: number--
The maximum size (in bytes) allowed for a single shard. Data exceeding this size after initial JSON encoding will trigger the sharding process.
key: string--
The primary key under which the file metadata (or the full data if not sharded) will be conceptually associated. This key is not directly used for storing shards.
userIds: {number}?--
An optional array of UserIDs for DataStore tagging.
}Parameters required for the write function.
WriteError
interface WriteError {error: string--
A string describing the error.
file: File--
The file metadata that was being processed when the error occurred. This is used for cleanup operations if shards were partially written.
}Structure representing an error encountered during the write operation.
ReadParams
interface ReadParams {}Parameters required for the read function.
Functions
splitString
Files.splitString(str: string,--
The string to be split.
chunkSize: number--
The size of each chunk.
) → {string}--
A table containing the split chunks.
Splits a string into chunks of a specified size. Used for sharding large data blobs into smaller pieces.
isLargeFile
Files.isLargeFile() → boolean--
True if the file is sharded, false otherwise.
Checks if a file object represents a sharded file (i.e., data stored across multiple keys).
write
Files.write() → Promise<File>--
A Promise that resolves with a File object representing the stored data (either directly containing the data or shard metadata).
Writes data to the DataStore, automatically handling sharding and compression if necessary.
If the JSON-encoded data is smaller than maxShardSize, it's stored directly within the
returned File object (in the data field).
If the data is larger, it's compressed, sharded, and stored across multiple DataStore keys.
The returned File object will contain shard (the unique ID for the shards) and
count (the number of shards) instead of the data field.
Errors
| Type | Description |
|---|---|
| WriteError | Rejects with a `WriteError` if any shard fails to write. |
| string | Propagates errors from `DataStore:SetAsync` via `dataStoreRetry`. |
read
Files.read() → Promise<any>--
A Promise that resolves with the original data.
Reads data from the DataStore, automatically handling reconstruction from shards if necessary.
If the provided file object contains the data field directly, it returns that data.
If the file object contains shard and count fields, it reads all corresponding shards
from the DataStore, concatenates them, decompresses the result, and returns the original data.
Errors
| Type | Description |
|---|---|
| string | Rejects with an error message string if any shard is missing or if decoding/decompression fails. Propagates errors from `DataStore:GetAsync` via `dataStoreRetry`. |