Skip to main content

DataStore

This item only works when running on the server. Server

Wraps the datastore object to provide async cached loading and saving. See DataStoreStage for more API.

Has the following features

  • Automatic saving every 5 minutes
  • Jitter (doesn't save all at the same time)
  • De-duplication (only updates data it needs)
  • Battle tested across multiple top games.
local playerMoneyValue = Instance.new("IntValue")
playerMoneyValue.Value = 0

local dataStore = DataStore.new(DataStoreService:GetDataStore("test"), test-store")
dataStore:Load("money", 0):Then(function(money)
	playerMoneyValue.Value = money
	dataStore:StoreOnValueChange("money", playerMoneyValue)
end):Catch(function()
	-- TODO: Notify player
end)

To use a datastore for a player, it's recommended you use the PlayerDataStoreService. This looks something like this. See ServiceBag for more information on service initialization.

local serviceBag = ServiceBag.new()
local playerDataStoreService = serviceBag:GetService(require("PlayerDataStoreService"))

serviceBag:Init()
serviceBag:Start()

local topMaid = Maid.new()

local function handlePlayer(player)
	local maid = Maid.new()

	local playerMoneyValue = Instance.new("IntValue")
	playerMoneyValue.Name = "Money"
	playerMoneyValue.Value = 0
	playerMoneyValue.Parent = player

	maid:GivePromise(playerDataStoreService:PromiseDataStore(Players)):Then(function(dataStore)
		maid:GivePromise(dataStore:Load("money", 0))
			:Then(function(money)
				playerMoneyValue.Value = money
				maid:GiveTask(dataStore:StoreOnValueChange("money", playerMoneyValue))
			end)
	end)

	topMaid[player] = maid
end
Players.PlayerAdded:Connect(handlePlayer)
Players.PlayerRemoving:Connect(function(player)
	topMaid[player] = nil
end)
for _, player in pairs(Players:GetPlayers()) do
	task.spawn(handlePlayer, player)
end

Properties​

Saving​

DataStore.Saving: Signal<Promise>

Prop that fires when saving. Promise will resolve once saving is complete.

Functions​

new​

DataStore.new(
robloxDataStore: DataStore,
key: string
) → DataStore

Constructs a new DataStore. See DataStoreStage for more API.

SetCacheTime​

DataStore:SetCacheTime(cacheTimeSeconds: number?) → ()

Sets how long the datastore will cache for

GetFullPath​

DataStore:GetFullPath() → string

Returns the full path for the datastore

DidLoadFail​

DataStore:DidLoadFail() → boolean

Returns whether the datastore failed.

PromiseLoadSuccessful​

DataStore:PromiseLoadSuccessful() → Promise<boolean>

Returns whether the datastore has loaded successfully.\

Save​

DataStore:Save() → Promise

Saves all stored data.

Load​

DataStore:Load(
keyName: string,
defaultValue: any?
) → any?

Loads data. This returns the originally loaded data.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new DataStore. See [DataStoreStage] for more API.",
            "params": [
                {
                    "name": "robloxDataStore",
                    "desc": "",
                    "lua_type": "DataStore"
                },
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "DataStore"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 92,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        },
        {
            "name": "SetCacheTime",
            "desc": "Sets how long the datastore will cache for",
            "params": [
                {
                    "name": "cacheTimeSeconds",
                    "desc": "",
                    "lua_type": "number?"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 142,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        },
        {
            "name": "GetFullPath",
            "desc": "Returns the full path for the datastore",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 152,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        },
        {
            "name": "DidLoadFail",
            "desc": "Returns whether the datastore failed.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 160,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        },
        {
            "name": "PromiseLoadSuccessful",
            "desc": "Returns whether the datastore has loaded successfully.\\",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<boolean>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 177,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        },
        {
            "name": "Save",
            "desc": "Saves all stored data.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 189,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        },
        {
            "name": "Load",
            "desc": "Loads data. This returns the originally loaded data.",
            "params": [
                {
                    "name": "keyName",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "defaultValue",
                    "desc": "",
                    "lua_type": "any?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any?"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 221,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        }
    ],
    "properties": [
        {
            "name": "Saving",
            "desc": "Prop that fires when saving. Promise will resolve once saving is complete.",
            "lua_type": "Signal<Promise>",
            "source": {
                "line": 108,
                "path": "src/datastore/src/Server/DataStore.lua"
            }
        }
    ],
    "types": [],
    "name": "DataStore",
    "desc": "Wraps the datastore object to provide async cached loading and saving. See [DataStoreStage] for more API.\n\nHas the following features\n* Automatic saving every 5 minutes\n* Jitter (doesn't save all at the same time)\n* De-duplication (only updates data it needs)\n* Battle tested across multiple top games.\n\n```lua\nlocal playerMoneyValue = Instance.new(\"IntValue\")\nplayerMoneyValue.Value = 0\n\nlocal dataStore = DataStore.new(DataStoreService:GetDataStore(\"test\"), test-store\")\ndataStore:Load(\"money\", 0):Then(function(money)\n\tplayerMoneyValue.Value = money\n\tdataStore:StoreOnValueChange(\"money\", playerMoneyValue)\nend):Catch(function()\n\t-- TODO: Notify player\nend)\n```\n\nTo use a datastore for a player, it's recommended you use the [PlayerDataStoreService]. This looks\nsomething like this. See [ServiceBag] for more information on service initialization.\n\n```lua\nlocal serviceBag = ServiceBag.new()\nlocal playerDataStoreService = serviceBag:GetService(require(\"PlayerDataStoreService\"))\n\nserviceBag:Init()\nserviceBag:Start()\n\nlocal topMaid = Maid.new()\n\nlocal function handlePlayer(player)\n\tlocal maid = Maid.new()\n\n\tlocal playerMoneyValue = Instance.new(\"IntValue\")\n\tplayerMoneyValue.Name = \"Money\"\n\tplayerMoneyValue.Value = 0\n\tplayerMoneyValue.Parent = player\n\n\tmaid:GivePromise(playerDataStoreService:PromiseDataStore(Players)):Then(function(dataStore)\n\t\tmaid:GivePromise(dataStore:Load(\"money\", 0))\n\t\t\t:Then(function(money)\n\t\t\t\tplayerMoneyValue.Value = money\n\t\t\t\tmaid:GiveTask(dataStore:StoreOnValueChange(\"money\", playerMoneyValue))\n\t\t\tend)\n\tend)\n\n\ttopMaid[player] = maid\nend\nPlayers.PlayerAdded:Connect(handlePlayer)\nPlayers.PlayerRemoving:Connect(function(player)\n\ttopMaid[player] = nil\nend)\nfor _, player in pairs(Players:GetPlayers()) do\n\ttask.spawn(handlePlayer, player)\nend\n```",
    "realm": [
        "Server"
    ],
    "source": {
        "line": 65,
        "path": "src/datastore/src/Server/DataStore.lua"
    }
}