Skip to main content

PlayerDataStoreManager

This item only works when running on the server. Server

DataStore manager for player that automatically saves on player leave and game close.

tip

Consider using PlayerDataStoreService instead, which wraps one PlayerDataStoreManager.

This will ensure that the datastores are reused between different services and other things integrating with Nevermore.

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

Functions

new

PlayerDataStoreManager.new(
robloxDataStoreDataStore,
keyGenerator(player) → string,--

Function that takes in a player, and outputs a key

skipBindingToCloseboolean?
) → PlayerDataStoreManager

Constructs a new PlayerDataStoreManager.

DisableSaveOnCloseStudio

PlayerDataStoreManager:DisableSaveOnCloseStudio() → ()

For if you want to disable saving in studio for faster close time!

AddRemovingCallback

PlayerDataStoreManager:AddRemovingCallback(
callbackfunction--

May return a promise

) → ()

Adds a callback to be called before save on removal

RemovePlayerDataStore

PlayerDataStoreManager:RemovePlayerDataStore(playerPlayer) → ()

Callable to allow manual GC so things can properly clean up. This can be used to pre-emptively cleanup players.

GetDataStore

PlayerDataStoreManager:GetDataStore(playerPlayer) → DataStore

PromiseAllSaves

PlayerDataStoreManager:PromiseAllSaves() → Promise

Removes all player data stores, and returns a promise that resolves when all pending saves are saved.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new PlayerDataStoreManager.",
            "params": [
                {
                    "name": "robloxDataStore",
                    "desc": "",
                    "lua_type": "DataStore"
                },
                {
                    "name": "keyGenerator",
                    "desc": "Function that takes in a player, and outputs a key",
                    "lua_type": "(player) -> string"
                },
                {
                    "name": "skipBindingToClose",
                    "desc": "",
                    "lua_type": "boolean?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "PlayerDataStoreManager"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 75,
                "path": "src/datastore/src/Server/PlayerDataStoreManager.lua"
            }
        },
        {
            "name": "DisableSaveOnCloseStudio",
            "desc": "For if you want to disable saving in studio for faster close time!",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 114,
                "path": "src/datastore/src/Server/PlayerDataStoreManager.lua"
            }
        },
        {
            "name": "AddRemovingCallback",
            "desc": "Adds a callback to be called before save on removal",
            "params": [
                {
                    "name": "callback",
                    "desc": "May return a promise",
                    "lua_type": "function"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 124,
                "path": "src/datastore/src/Server/PlayerDataStoreManager.lua"
            }
        },
        {
            "name": "RemovePlayerDataStore",
            "desc": "Callable to allow manual GC so things can properly clean up.\nThis can be used to pre-emptively cleanup players.",
            "params": [
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 134,
                "path": "src/datastore/src/Server/PlayerDataStoreManager.lua"
            }
        },
        {
            "name": "GetDataStore",
            "desc": "",
            "params": [
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "DataStore"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 142,
                "path": "src/datastore/src/Server/PlayerDataStoreManager.lua"
            }
        },
        {
            "name": "PromiseAllSaves",
            "desc": "Removes all player data stores, and returns a promise that\nresolves when all pending saves are saved.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 163,
                "path": "src/datastore/src/Server/PlayerDataStoreManager.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "PlayerDataStoreManager",
    "desc": "DataStore manager for player that automatically saves on player leave and game close.\n\n:::tip\nConsider using [PlayerDataStoreService] instead, which wraps one PlayerDataStoreManager.\n:::\n\nThis will ensure that the datastores are reused between different services and other things integrating\nwith Nevermore.\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": 50,
        "path": "src/datastore/src/Server/PlayerDataStoreManager.lua"
    }
}