DataStoreService는 Roblox에서 개발자가 게임의 지속적인 데이터를 저장하고 검색할 수 있게 해주는 서비스입니다. 이는 플레이어 데이터, 게임 통계, 구성 설정 등을 저장하는 데 유용합니다. 데이터는 클라우드에 저장되며, 여러 게임 세션과 서버에서 접근할 수 있습니다.
입력 | 세부사항 |
---|---|
DataStoreService | DataStore에 접근하는 주요 서비스. |
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)