DataStorePlus - A Simple Yet Powerful Data-Saving module!

Introducing DataStorePlus!

DataStorePlus (hereafter referred to as DS+) is an advanced version of DataStoreService that does all the painful work for you so you won’t have to do them yourselves. It grants users easier control over their datastores.

Installation and Guide

Installation guide and API documents are available here at GitBook.

Features

DS+ has numerous advantages and features:

  • Simple and Easy → DS+ is easy to learn.
  • Auto-Caching → Cache retrieved data so you won’t have make calls again.
  • Reconcile → Set-up default value to replace empty information.
  • Session States → Freeze or Lock your stores to secure data.
  • Auto-Throttling → Built-in throttling system to run operations once available.
  • Shutdown Handling → DS+ will attempt to transfer operations to any live server when needed, if possible.

Basic Usage

I’ll show you how to use DS+ through a basic usage example:

local Players = game:GetService("Players")

local DataStorePlus = require(path.to.DataStorePlus)
local GlobalStore = DataStorePlus.LoadStore("MyStore")

-->> We set the default value here to replace the value returned by :Read() if it is nil.
GlobalStore:SetDefaultValue({
    Coins = 10,
    Gems = 0
})

local function playerJoined(player: Player)
    local store = GlobalStore:GetStore(tostring(player.UserId))
    -->> Here, we yield the store until it is accessible or a 20 second timeout has passed.
    local counter, loaded = 0, true
    repeat 
        counter += 1
        if counter >= 40 then
            loaded = false
        end
        task.wait(2) 
    until store:GetKeyState() ~= "Locked" or counter >= 10
    
    -->> Kick the player in case the store is still locked.
    if not loaded then
        player:Kick("Failed to load data, please rejoin later.")
    end
    
    -->> It shouldn't return nil as we've already set the default value.
    local data = store:Read()
    
    -->> do something with your data
end

-->> Since loading the GlobalStore might take some time, we should write this for security.
for _, player in Players:GetPlayers() do
    playerJoined(player)
end

Players.PlayedAdded:Connect(function(player)
    playerJoined(player)
end)

Players.PlayerRemoving:Connect(function(player)
    local value = {} -->> Input the data to save here
    
    local store = GlobalStore:GetStore(tostring(player.UserId))
    -->> Lock the store so other servers cannot read the unsaved data.
    local s, bypassId = store:SetKeyState("Locked", 30) 
    store:Write(value, bypassId)
    -->> Set the state back to normal after the writing operation has been done
    store:SetKeyState("Normal")
    store:End()
end)

Lastly, rating DS+ would help a lot!

What do you think?
  • Awesome module!
  • Pretty solid.
  • Not really that good. (Please leave suggestions!)
0 voters

Bug reports and constructive advice are highly appreciated!

Credits

Happy coding!

2 Likes