Basic Usage

Here, we'll be writing a basic player data example to help you understand the basics of DataStore+:

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
    
    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)

Last updated