Data Store

0

  • used to save data which should persist between game sessions
  • shared per game
  • essentially a dictionary
  • Each value can be indexed by a unique key which includes the player’s UserId
  • network calls may occasionally fail so wrap data store commands in pcall() to handle errors
  • Game Settings > Enable Studio Access to API Services
    • To avoid overwriting production data do not to enable this setting for live games
ServerScriptService/Script
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

-- set data
local success, err = pcall(function()
    experienceStore:SetAsync("Player_1234", 50)
end)

if success then
    print("Success!")
end

-- get data 
local success, currentExperience = pcall(function()
    return experienceStore:GetAsync("Player_1234")
end)

if success then
    print("Current Experience:", currentExperience)
end

-- update data
local nicknameStore = DataStoreService:GetDataStore("Nicknames")

local function makeNameUpper(currentName)
    local newName = string.upper(currentName)
    return newName
end

local success, newName = pcall(function()
    return nicknameStore:UpdateAsync("Player_1234", makeNameUpper)
end)

if success then
    print("Uppercase Name:", newName)
end

  • 0 posts
  • 1 subtopic
  • 10 days ago by vince