Advanced Usage

Now that you’ve gone through some basic examples of using DataStore+, let’s dive deeper and explore more about what DataStore+ could do.

Reverting data to a certain version

Let’s think about this: Your players reported a data corruption bug and they demand data recovery. You could do this by resetting the players’ data to the version before the corruption event.

Let's say the incident happened at 00:00 on 1 March, 2025, we could write like this:

local DataStorePlus = require(path.to.DataStorePlus)
local GlobalStore = DataStorePlus.LoadStore("MyStore") -->> Load the GlobalStore

local corruptionTime = DateTime.new(2025, 3)
local key = "YOUR_KEY_HERE"

-->> Get the store of key
local store = GlobalStore:GetStore(key)
local versions = store:ListVersions(true, nil, corruptionTime)

-->> Loop through the versions, and get the second closest one 
local count, VersionId, value = 1, nil, nil
for versionId, createdTime in pairs(versions) do
    count += 1
    if count >= 2 then
        VersionId = versionId
        break 
    end
end

-->> This will only be called if there are more than 1 versions.
if versionId then
    value = store:GetVersionWithId(versionId)
else -->> And this will be called if not
    warn("Less than 2 versions were found for key "..key.."."
    value = store:GetVersion(corruptionTime) -->> We'll use the lastest version, although risky..
end

if value then
    store:Write(value)
end

And there we go! We've finished the process, hopefully your players will get their data back.

Last updated