DataStoreService

DataStoreService 是 Roblox 中的一項服務,允許開發者存儲和檢索持久性數據,這對於保存玩家數據、遊戲統計數據、配置設置等非常有用。這些數據存儲在雲端,可以跨不同的遊戲會話和伺服器訪問。
輸入詳情
DataStoreService訪問 DataStores 的主要服務。
DataStoreDataStoreService 中存儲數據的存儲單元。
GetAsync從 DataStore 中檢索數據。
SetAsync將數據保存到 DataStore 中。
UpdateAsync通過應用轉換函數更新 DataStore 中的數據。

DataStoreService 權限

Roblox Studio
>
File
>
Game Settings
>
Security
>
Enable Studio Access to API Services
>
Yes
Image 1
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)
Image 1
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)
Image 1
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)