DataStoreService
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Dịch vụ lưu trữ dữ liệu tiết lộ các phương pháp để lấy GlobalDataStore và OrderedDataStore đối tượng.Các kho dữ liệu chỉ có thể truy cập bởi các máy chủ trò chơi, vì vậy bạn chỉ có thể sử dụng DataStoreService trong một Script hoặc một ModuleScript được sử dụng bởi một Script .
Xem Kho lưu trữ dữ liệu để có hướng dẫn chi tiết về cấu trúc dữ liệu, quản lý, xử lý lỗi, v.v.
Mẫu mã
This code sample prints the request budget for all data store request types.
local DataStoreService = game:GetService("DataStoreService")
for _, enumItem in pairs(Enum.DataStoreRequestType:GetEnumItems()) do
print(enumItem.Name, DataStoreService:GetRequestBudgetForRequestType(enumItem))
end
Tóm Tắt
Thuộc Tính
Xác định xem chức năng lưu trữ dữ liệu có nên tự động thử lại hay không.DataStoreService không tôn trọng thuộc tính này vì lý do kỹ thuật đã vô hiệu hóa việc thử lại tự động.Do đó, bạn phải triển khai các hệ thống để thực hiện lại các hoạt động của riêng mình.
Phương Pháp
Tạo một DataStore với tên và phạm vi được cung cấp.
Trả lại kho lưu trữ dữ cửa hàngmặc định.
Nhận một OrderedDataStore được cho một tên và phạm vi tùy chọn.
Trả về số lượng yêu cầu có thể được thực hiện bởi đánh máyyêu cầu đã cho.
Trả về một đối tượng DataStoreListingPages để liệt kê qua tất cả các kho dữ liệu trải nghiệm
Thuộc Tính
AutomaticRetry
Phương Pháp
GetDataStore
Tham Số
Lợi Nhuận
GetGlobalDataStore
Lợi Nhuận
Mẫu mã
local DataStoreService = game:GetService("DataStoreService")
local GlobalDataStore = DataStoreService:GetGlobalDataStore()
print(GlobalDataStore.Name)
GetOrderedDataStore
Tham Số
Lợi Nhuận
Mẫu mã
local DataStoreService = game:GetService("DataStoreService")
local pointsStore = DataStoreService:GetOrderedDataStore("Points")
local function printTopTenPlayers()
local isAscending = false
local pageSize = 10
local pages = pointsStore:GetSortedAsync(isAscending, pageSize)
local topTen = pages:GetCurrentPage()
-- The data in 'topTen' is stored with the index being the index on the page
-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
for rank, data in ipairs(topTen) do
local name = data.key
local points = data.value
print(name .. " is ranked #" .. rank .. " with " .. points .. "points")
end
-- Potentially load the next page...
--pages:AdvanceToNextPageAsync()
end
-- Create some data
pointsStore:SetAsync("Alex", 55)
pointsStore:SetAsync("Charley", 32)
pointsStore:SetAsync("Sydney", 68)
-- Display the top ten players
printTopTenPlayers()
GetRequestBudgetForRequestType
Tham Số
Lợi Nhuận
Mẫu mã
local DataStoreService = game:GetService("DataStoreService")
local globalStore = DataStoreService:GetGlobalDataStore()
local function printBudget()
local budget = DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.SetIncrementAsync)
print("Current set/increment budget:", budget)
end
for i = 1, 5 do
local key = "key" .. i
local success, err = pcall(function()
globalStore:SetAsync(key, true)
end)
if success then
printBudget()
else
print(err)
end
end