Hello all,
This is a tutorial on how to compress your datastores by as much as 400% using the LZ4 Library and Buffer.
Prerequisites
LZ4 Library
Step 1: Have a large amount of text you want to compress.
This string is about 35000 characters long.
Step 2: Using the LZ4 library, compress the string using .compress() . The output should look something like this.
local LZ4 = require(script.LZ4)
local LZ4compressed = LZ4.compress(String)
This string is only about 8700 characters long.
Step 3: Compress the string again using buffer.fromstring()
and you should get something like this.
buffer: 0xae543138174fb25f
local LZ4 = require(script.LZ4)
local LZ4compressed = LZ4.compress(String)
print(buffer.fromstring(LZ4compressed))
Step 4: Save this buffer into the database you want and to should do something like this
local yourDataStore = game:GetService("DataStoreService"):GetDataStore("String")
local LZ4 = require(script.LZ4)
local LZ4compressed = LZ4.compress(String)
yourDataStore:SetAsync("String",buffer.fromstring(LZ4compressed))
local buffercompressedData = yourDataStore:GetAsync("String")
local LZ4compressedData = buffer.tostring(buffercompressedData)
print(LZ4.decompress(LZ4compressedData))
We can save tables in LZ4 using game.HttpService:JSONEncode
, here’s an example.
local yourDataStore = game:GetService("DataStoreService"):GetDataStore("String")
local Table = {
["Lorem"] = {"ipsum"},
sum = 124124,
{}
}
local LZ4 = require(script.LZ4)
local LZ4compressed = LZ4.compress(game.HttpService:JSONEncode(Table))
yourDataStore:SetAsync("String",buffer.fromstring(LZ4compressed))
local buffercompressedData = yourDataStore:GetAsync("String")
local LZ4compressedData = buffer.tostring(buffercompressedData)
print(game.HttpService:JSONDecode(LZ4.decompress(LZ4compressedData))) -- {["Lorem"]={"ipsum"},sum = 124124,{}}
Credits to @metatablecatmaid for the port of LZ4!
LZ4 Download Link:
lz4.lua (7.0 KB)