DataStoreServiceクラス
ひとことで言うと
プレイヤーのコインやレベルなどのデータを、ゲームを終了しても残るように保存するためのサービスです。
公式の説明(英語):A game service that gives access to persistent data storage across places in a game.
データの保存と読み込みは、通信の失敗で止まることがあるため、pcallで包んで使うのが基本です。Roblox Studioで試すときは、ゲームの設定でAPIサービスへのアクセスを有効にする必要があります。
使用例
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local coinStore = DataStoreService:GetDataStore("PlayerCoins")
Players.PlayerAdded:Connect(function(player)
local success, coins = pcall(function()
return coinStore:GetAsync(tostring(player.UserId))
end)
if success then
print(player.Name .. " のコイン:" .. tostring(coins or 0))
else
warn("データを読み込めませんでした")
end
end)
Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
coinStore:SetAsync(tostring(player.UserId), 100)
end)
if not success then
warn("データを保存できませんでした:" .. tostring(err))
end
end)スクリプトは、Luauの型チェック(luau-lsp)で確認しています。
クラスの関係
メソッド(6)
| 名前と書き方 | 説明 |
|---|---|
GetDataStoreDataStoreService:GetDataStore(name: string, scope: string, options: Instance): DataStore | 名前を指定して、データを保存する場所(データストア)を取り出します。Creates a DataStore instance with the provided name and scope. |
GetGlobalDataStoreDataStoreService:GetGlobalDataStore(): DataStore | Returns the default data store. |
GetOrderedDataStoreDataStoreService:GetOrderedDataStore(name: string, scope: string): OrderedDataStore | Get an OrderedDataStore given a name and optional scope. |
GetRequestBudgetForRequestTypeDataStoreService:GetRequestBudgetForRequestType(requestType: DataStoreRequestType): int | Returns the number of requests that can be made by the given request type. |
ListDataStoresAsyncDataStoreService:ListDataStoresAsync(prefix: string, pageSize: int, cursor: string): DataStoreListingPages | Returns a DataStoreListingPages object for enumerating through all of the experience's data stores. |
SetRateLimitForRequestTypeDataStoreService:SetRateLimitForRequestType(requestType: DataStoreRequestType, baseLimit: int, perPlayerLimit: int): () | Sets the rate limit for a given request type per minute. |
受け継いでいるメンバー
Instance から受け継いでいるもの(46)
Archivable・Capabilities・IsInSandbox・Name・Parent・PredictionMode・Sandboxed・AddTag・ClearAllChildren・Clone・Destroy・FindFirstAncestor・FindFirstAncestorOfClass・FindFirstAncestorWhichIsA・FindFirstChild・FindFirstChildOfClass・FindFirstChildWhichIsA・FindFirstDescendant・GetActor・GetAttribute・GetAttributeChangedSignal・GetAttributes・GetChildren・GetDebugId・GetDescendants・GetFullName・GetStyled・GetStyledPropertyChangedSignal・GetTags・HasTag・IsAncestorOf・IsDescendantOf・IsPropertyModified・QueryDescendants・RemoveTag・ResetPropertyToDefault・SetAttribute・WaitForChild・AncestryChanged・AttributeChanged・ChildAdded・ChildRemoved・DescendantAdded・DescendantRemoving・Destroying・StyledPropertiesChanged
Object から受け継いでいるもの(4)
DataStoreServiceを使う記事
英語の説明とメンバーの一覧は、Roblox Creator Documentation(DataStoreService)をもとにしています(CC BY 4.0)。日本語の解説は、このサイトで独自に書いたものです。