roblox-datastores
它能做什么
该技能讲解如何在 Roblox 中保存玩家进度,使其在不同游戏会话之间保留。适合希望金币、等级或物品能够持久保存的开发者。
适用场景
- ✓当你需要在 Roblox 中保存玩家的金币、等级或物品栏,使其跨会话保留时。
- ✓当玩家的数据丢失,或玩家离开时数据被覆盖时。
- ✓当你想在 Roblox 中建立持久的排行榜时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/other-engines/roblox-datastores/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请让我的 Roblox 游戏在玩家离开后再回来时,记住每位玩家的金币。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解玩家加入时读取数据、玩家离开或服务器关闭时保存数据,将每次调用包装起来以处理失败情况,并为排行榜使用重试和有序存储。
技术内容(原始 SKILL.md)
作者在 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, orGetOrderedDataStore.
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
- 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, neverLocalScripts. - Get a store, then read/write by key.
DataStoreService:GetDataStore("Name"); key per player is usually"Player_" .. player.UserId. - Wrap every call in
pcall.GetAsync/SetAsync/UpdateAsyncare network calls that can fail; an unguarded failure errors the thread and risks data loss. - Load on
PlayerAdded, save onPlayerRemoving, and alsoBindToClose. A leaving player and a shutting-down server both need a final save. - Prefer
UpdateAsyncfor read-modify-write (multi-server safe) overSetAsync(blind overwrite). On a failed load, do not overwrite with defaults — abort the save so you don't wipe good data. - Use
OrderedDataStorefor ranked data (leaderboards) viaGetSortedAsync. 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
常见问题
DataStore 能在本地脚本中使用吗?
不能。该技能指出,DataStore 只能在服务器脚本中使用。
为什么要开启 Studio 访问?
该技能要求开启 Studio 对 API 服务的访问权限,使 Studio 能够使用实时数据,并为此使用单独的测试场地。
它适用于临时的对局状态吗?
不适用。临时状态使用另一种服务,本技能涵盖的是保存的数据。
详情
作者的原始描述
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.
热度
- GitHub 星标
- 6.6k
- 排名
- #13837
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …