Langsung ke konten
Skillpedia
Pemrograman umum6.6k Β· #13837

roblox-datastores

Fungsinya

Skill ini menjelaskan cara menyimpan kemajuan pemain di Roblox agar tetap ada di antara sesi bermain. Skill ini ditujukan untuk developer yang ingin koin, level, atau item tetap tersimpan.

Kapan dipakai

  • βœ“Saat Anda perlu menyimpan koin, level, atau inventaris pemain di antara sesi di Roblox.
  • βœ“Saat pemain kehilangan data atau data tertimpa ketika mereka keluar.
  • βœ“Saat Anda ingin membangun papan peringkat yang tetap ada di Roblox.

Link skill

https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/other-engines/roblox-datastores/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/other-engines/roblox-datastores/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBuat game Roblox saya mengingat koin setiap pemain setelah mereka keluar dan kembali.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill menjelaskan cara memuat data saat pemain bergabung, menyimpan saat pemain keluar atau server dimatikan, membungkus setiap pemanggilan agar kegagalan tertangani, serta memakai percobaan ulang dan ordered store untuk peringkat.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Roblox DataStores

Persist data across sessions in Roblox with DataStoreService: loading on join, saving on leave and shutdown, safe updates, retries, and ordered stores for leaderboards. Server-side only.

When to use

  • Use to save/load player progress (coins, inventory, levels), build persistent leaderboards, or fix data loss, overwrites, and throttling.
  • Use when server code calls DataStoreService, GetDataStore, GetAsync, SetAsync, UpdateAsync, or GetOrderedDataStore.

When not to use: general scripting, services, remotes, the client/server split β†’ roblox-luau. High-frequency temporary state (matchmaking, per-round) β†’ memory stores (a different service). Engine-agnostic persistence theory β†’ save-systems.

Core workflow

  1. Enable Studio access once. In a published place, File β†’ Experience Settings β†’ Security β†’ Enable Studio Access to API Services (use a separate test place; Studio hits the same live data as players). DataStores work only from server Scripts, never LocalScripts.
  2. Get a store, then read/write by key. DataStoreService:GetDataStore("Name"); key per player is usually "Player_" .. player.UserId.
  3. Wrap every call in pcall. GetAsync/SetAsync/UpdateAsync are network calls that can fail; an unguarded failure errors the thread and risks data loss.
  4. Load on PlayerAdded, save on PlayerRemoving, and also BindToClose. A leaving player and a shutting-down server both need a final save.
  5. Prefer UpdateAsync for read-modify-write (multi-server safe) over SetAsync (blind overwrite). On a failed load, do not overwrite with defaults β€” abort the save so you don't wipe good data.
  6. Use OrderedDataStore for ranked data (leaderboards) via GetSortedAsync. Test by joining, changing data, rejoining, and confirming it persisted.

Patterns

1. Load on join (pcall-guarded)

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local store = DataStoreService:GetDataStore("PlayerData")

local DEFAULT = { Coins = 0, Level = 1 }

Players.PlayerAdded:Connect(function(player)
    local key = "Player_" .. player.UserId
    local ok, data = pcall(function()
        return store:GetAsync(key)
    end)

    if not ok then
        -- Load FAILED (network). Do not treat as a new player; flag so we never save
        -- over their real data with defaults.
        warn("Load failed for", player.Name, data)
        player:SetAttribute("DataLoaded", false)
        return
    end

    player:SetAttribute("DataLoaded", true)
    local profile = data or DEFAULT          -- nil == genuinely new player
    applyToLeaderstats(player, profile)
end)

2. Save with UpdateAsync (multi-server safe)

-- UpdateAsync reads the latest value, then writes what the callback returns.
-- The callback MUST NOT yield (no task.wait, no further Async calls inside it), and it
-- can run more than once if another server writes the key first, so keep it pure.
local function savePlayer(player)
    -- Save only after a confirmed load. `~= true` also covers a player who leaves while
    -- GetAsync is still in flight (attribute still nil), which `== false` would miss.
    if player:GetAttribute("DataLoaded") ~= true then return end
    local key = "Player_" .. player.UserId
    local newData = gatherDataFor(player)    -- a plain table of serializable values

    local ok, err = pcall(function()
        store:UpdateAsync(key, function(old)
            -- merge/decide here; return nil to cancel the write
            return newData
        end)
    end)
    if not ok then warn("Save failed for", player.Name, err) end
end

3. Save on leave AND on shutdown

Players.PlayerRemoving:Connect(savePlayer)

-- BindToClose runs when the server shuts down; save everyone still in.
-- The server waits at most 30 seconds for it, so save in parallel and yield until done.
game:BindToClose(function()
    local players = Players:GetPlayers()
    local remaining = #players
    if remaining == 0 then return end
    for _, player in players do
        task.spawn(function()
            savePlayer(player)
            remaining -= 1
        end)
    end
    while remaining > 0 do task.wait() end
end)

4. Retry with backoff (transient failures)

local function withRetry(fn, attempts)
    attempts = attempts or 3
    for i = 1, attempts do
        local ok, result 
Pertanyaan umum

Apakah DataStore berfungsi di skrip lokal?

Tidak. Skill menyebutkan bahwa DataStore hanya berfungsi dari skrip server.

Mengapa mengaktifkan akses Studio?

Skill menyebutkan untuk mengaktifkan akses Studio ke layanan API agar Studio dapat memakai data langsung, dan memakai tempat uji terpisah untuk itu.

Apakah ini untuk state pertandingan sementara?

Tidak. State sementara memakai layanan lain, dan skill ini mencakup data yang tersimpan.

Detail

Deskripsi asli dari pembuat

Persist player data in Roblox with DataStoreService: GetDataStore, GetAsync/ SetAsync/UpdateAsync/IncrementAsync wrapped in pcall, load-on-join and save-on-leave plus BindToClose, retries, and OrderedDataStore leaderboards. Use when saving or loading persistent data in a Roblox experience β€” when the user mentions DataStore, DataStoreService, GetAsync, SetAsync, UpdateAsync, save player data, or leaderboards. For general Luau scripting use roblox-luau.

repo: gamedev-skills/awesome-gamedev-agent-skills

Popularitas

Bintang GitHub
6.6k
Peringkat
#13837
Lisensi
Apache-2.0
Diperbarui
28/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa