DataStoreService 是 Roblox 中的一项服务,允许开发者存储和检索游戏的持久性数据。这对于保存玩家数据、游戏统计、配置设置等非常有用。这些数据存储在云端,可以跨游戏会话和服务器访问。
输入 | 详情 |
---|---|
DataStoreService | 访问 DataStores 的主要服务。 |
DataStore | DataStoreService 中存储数据的存储单元。 |
GetAsync | 从 DataStore 中检索数据。 |
SetAsync | 将数据保存到 DataStore 中。 |
UpdateAsync | 通过应用转换函数更新 DataStore 中的数据。 |
DataStoreService 权限
Roblox Studio
>File
>Game Settings
>Security
>Enable Studio Access to API Services
>Yes
Roblox Studio
创建名为 'PlayerData' 的 DataStore
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
示例: 更新数据
game.Players.PlayerRemoving:Connect(function(player)
local playerKey = "Player_" .. player.UserId
local data = {
Example_String_Field = "Example_Value",
Example_Number_Field = 123,
Example_Bool_Field = true,
Example_Timestamp_Field = os.time()
}
local success, errorMessage = pcall(function()
playerDataStore:SetAsync(playerKey, data)
end)
if success then
print("Player data saved successfully for player: " .. player.Name)
else
warn("Failed to save player data for player: " .. player.Name .. ". Error: " .. errorMessage)
end
end)
Roblox Studio
示例: 加载数据
game.Players.PlayerAdded:Connect(function(player)
local playerKey = "Player_" .. player.UserId
local success, data = pcall(function()
return playerDataStore:GetAsync(playerKey)
end)
if success and data then
-- Assuming that you have already set up leaderstats with Level and Experience
player.leaderstats.Level.Value = data.Level or 1 -- Provide a default value if nil
player.leaderstats.Experience.Value = data.Experience or 0 -- Provide a default value if nil
-- Print out the stored data
print("Player data loaded successfully for player: " .. player.Name)
print("Example_String_Field: " .. (data.Example_String_Field or "N/A"))
print("Example_Number_Field: " .. (data.Example_Number_Field or "N/A"))
print("Example_Bool_Field: " .. tostring(data.Example_Bool_Field or "N/A"))
print("Example_Timestamp_Field: " .. os.date("%c", data.Example_Timestamp_Field or 0)) -- Convert timestamp to a readable format
else
warn("Failed to load player data for player: " .. player.Name)
end
end)
Roblox Studio
示例: 定期自动保存数据
local function savePlayerData(player)
local playerKey = "Player_" .. player.UserId
local success, errorMessage = pcall(function()
playerDataStore:UpdateAsync(playerKey, function(oldData)
oldData = oldData or {
Example_String_Field = "Default_String",
Example_Number_Field = 0,
Example_Bool_Field = false,
Example_Timestamp_Field = os.time()
}
-- Update all fields with the data from the player object or default values
oldData.Example_String_Field = player.Example_String_Field or oldData.Example_String_Field
oldData.Example_Number_Field = player.Example_Number_Field or oldData.Example_Number_Field
oldData.Example_Bool_Field = player.Example_Bool_Field or oldData.Example_Bool_Field
oldData.Example_Timestamp_Field = os.time() -- Always update the timestamp to current time
return oldData
end)
end)
if success then
print("Player data saved successfully for player: " .. player.Name)
else
warn("Failed to save player data for player: " .. player.Name .. ". Error: " .. errorMessage)
end
end
-- Function to periodically save data
local function startAutoSave(player)
while player.Parent do
wait(5) -- Wait 1 minute
savePlayerData(player)
end
end
-- Connect auto-save function to player joining
game.Players.PlayerAdded:Connect(function(player)
-- Start auto-saving for this player
coroutine.wrap(startAutoSave)(player)
end)
-- Ensure data is saved when the player leaves as a fallback
game.Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)