要点
在 DataStore 中为每个玩家保存两个数字:last,即上次领取时的 os.time() 值,以及 streak。过了 24 小时就可以领取。如果超过了 48 小时,连续天数从第 1 天重新开始;否则增加一天,并在 7 天里循环,奖励逐日递增。把检查放在 UpdateAsync 里做,让判断和保存同时发生。
- 在服务端使用
os.time(),不要使用客户端发来的时间。 - 在 UpdateAsync 内部,不能领取时返回 nil:保存会被取消,什么也不发放。
- 客户端只查询状态并请求领取;由服务端来回答。
每日奖励和连续签到的工作方式
| 距上次领取的时间 | 结果 |
|---|---|
| 少于 24 小时 | 还太早。按钮显示倒计时。 |
| 24 到 48 小时 | 可以领取,连续天数加一天。 |
| 超过 48 小时 | 可以领取,连续天数从第 1 天重新开始。 |
| 第 7 天之后 | 下一次领取是新一周的第 1 天。 |
服务端的 os.time() 返回以秒为单位的当前 Unix 时间(UTC),所以所有服务器的结果都一致。玩家电脑上的时钟不参与计算,因此在手机上改日期没有任何用处。
第 1 步:奖励服务端
在 Game Settings 的 Security 中开启 Enable Studio Access to API Services(场景必须已发布)。然后在 ServerScriptService 中插入一个名为 DailyRewards 的 Script:
-- ServerScriptService 中名为 DailyRewards 的 Script。
-- 每 24 小时一次奖励。如果在 48 小时内再次领取,连续天数继续,
-- 奖励在一周内每天递增,之后新的一周重新开始。
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local REWARDS = { 50, 75, 100, 150, 200, 300, 500 } -- 连续第 1 到第 7 天的金币
local CLAIM_EVERY = 24 * 60 * 60 -- 两次领取之间的秒数
local STREAK_LOST_AFTER = 48 * 60 * 60 -- 错过了一整天,连续天数重新开始
type Record = { last: number, streak: number }
local store = DataStoreService:GetDataStore("DailyRewards_v1")
local statusFunction = Instance.new("RemoteFunction")
statusFunction.Name = "GetDailyStatus"
statusFunction.Parent = ReplicatedStorage
local claimFunction = Instance.new("RemoteFunction")
claimFunction.Name = "ClaimDaily"
claimFunction.Parent = ReplicatedStorage
local records: { [Player]: Record } = {}
local claiming: { [Player]: boolean } = {}
local function readRecord(data: any): Record
if type(data) == "table" and type(data.last) == "number" and type(data.streak) == "number" then
return { last = data.last, streak = data.streak }
end
return { last = 0, streak = 0 }
end
-- 下一次领取对应的连续天数,从 1 到 7
local function nextDay(record: Record, now: number): number
if now - record.last >= STREAK_LOST_AFTER then
return 1
end
return record.streak % #REWARDS + 1
end
local function addCoins(player: Player, amount: number)
local leaderstats = player:FindFirstChild("leaderstats")
local coins = leaderstats and leaderstats:FindFirstChild("Coins")
if coins and coins:IsA("IntValue") then
coins.Value += amount
end
end
Players.PlayerAdded:Connect(function(player: Player)
local ok, data = pcall(function()
return store:GetAsync(tostring(player.UserId))
end)
if ok then
records[player] = readRecord(data)
else
warn("DailyRewards: could not load", player.Name, data)
end
end)
Players.PlayerRemoving:Connect(function(player: Player)
records[player] = nil
claiming[player] = nil
end)
-- 菜单显示的内容:距下次领取的秒数(0 = 现在就能领)以及是第几天
statusFunction.OnServerInvoke = function(player: Player)
local record = records[player]
if not record then
return nil
end
local now = os.time()
return {
wait = math.max(0, record.last + CLAIM_EVERY - now),
day = nextDay(record, now),
rewards = REWARDS,
}
end
claimFunction.OnServerInvoke = function(player: Player): string
local record = records[player]
if not record then
return "Still loading, try again in a moment"
end
if claiming[player] then
return "One moment..."
end
claiming[player] = true
local now = os.time()
local claimedDay: number? = nil
-- UpdateAsync 读取最新保存的记录,并在更新内部做决定,所以
-- 第二个服务器(或第二次点击)不能把同一天领取两次
local ok, err = pcall(function()
store:UpdateAsync(tostring(player.UserId), function(old: any)
-- UpdateAsync 可能多次调用这个函数:只有最后一次调用算数
claimedDay = nil
local saved = readRecord(old)
if now - saved.last < CLAIM_EVERY then
return nil -- 还太早:返回 nil 会取消保存
end
local day = nextDay(saved, now)
claimedDay = day
return { last = now, streak = day }
end)
end)
claiming[player] = nil
if not ok then
warn("DailyRewards: could not save", player.Name, err)
return "Could not save, try again later"
end
local day = claimedDay
if not day then
return "Come back later for your next reward"
end
records[player] = { last = now, streak = day }
addCoins(player, REWARDS[day])
return string.format("Day %d reward: +%d coins!", day, REWARDS[day])
end
领取发生在传给 UpdateAsync 的函数内部。Roblox 会用最新保存的记录调用它。如果还太早,函数返回 nil,Roblox 就取消写入。否则它返回新的记录,脚本记下领取的是第几天。只有在 UpdateAsync 没有出错地完成之后,才会加金币。如果另一个服务器在此期间修改了这个键,Roblox 可能会用更新的值再次调用这个函数,所以判断才要放在函数里面做。
第 2 步:带倒计时的按钮
在 StarterPlayer > StarterPlayerScripts 中插入一个名为 DailyRewardsMenu 的 LocalScript:
-- StarterPlayerScripts 中名为 DailyRewardsMenu 的 LocalScript。
-- 带倒计时的 Daily 按钮。奖励准备好时,按钮上显示 Claim。
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local statusFunction = ReplicatedStorage:WaitForChild("GetDailyStatus") :: RemoteFunction
local claimFunction = ReplicatedStorage:WaitForChild("ClaimDaily") :: RemoteFunction
local gui = Instance.new("ScreenGui")
gui.Name = "DailyGui"
gui.ResetOnSpawn = false
local button = Instance.new("TextButton")
button.Size = UDim2.fromOffset(170, 48)
button.Position = UDim2.new(0, 12, 0.5, 52)
button.BackgroundColor3 = Color3.fromRGB(240, 170, 40)
button.TextColor3 = Color3.new(1, 1, 1)
button.Font = Enum.Font.GothamBold
button.TextSize = 17
button.Text = "Daily reward"
local corner = Instance.new("UICorner")
corner.CornerRadius = UDim.new(0, 10)
corner.Parent = button
button.Parent = gui
gui.Parent = Players.LocalPlayer:WaitForChild("PlayerGui")
local readyAt: number? = nil -- 奖励准备好时的 os.clock() 时间
local day = 1
local busy = false
local function refresh()
local ok, status = pcall(function()
return statusFunction:InvokeServer()
end)
if ok and type(status) == "table" and type(status.wait) == "number" then
readyAt = os.clock() + status.wait
day = if type(status.day) == "number" then status.day else 1
end
end
local function format(seconds: number): string
local h = seconds // 3600
local m = (seconds % 3600) // 60
local s = seconds % 60
return string.format("%02d:%02d:%02d", h, m, s)
end
button.Activated:Connect(function()
if busy or not readyAt or os.clock() < readyAt then
return
end
busy = true
button.Text = "Claiming..."
local ok, message = pcall(function()
return claimFunction:InvokeServer()
end)
button.Text = if ok and type(message) == "string" then message else "Try again"
task.wait(2)
refresh()
busy = false
end)
refresh()
while true do
if not busy then
if not readyAt then
button.Text = "Loading..."
refresh()
elseif os.clock() >= readyAt then
button.Text = string.format("Claim day %d!", day)
else
button.Text = "Next: " .. format(math.ceil(readyAt - os.clock()))
end
end
task.wait(1)
end
按钮向服务端询问一次要等多久,然后自己用 os.clock() 每秒倒数,所以不会每秒都去请求服务端。领取之后它会再问一次。倒计时只是为了显示:玩家点击 Claim 时,服务端仍然会检查时间。
测试一下
- 测试时把
CLAIM_EVERY设为30,把STREAK_LOST_AFTER设为60。 - 点击 Play。按钮上显示“Claim day 1!”。点击它:+50 金币。
- 按钮倒数 30 秒。在接下来的 30 秒内再次领取:第 2 天,+75。
- 等待超过一分钟:下一次领取又是第 1 天。
- 发布前把两个值改回 24 小时和 48 小时的那两行。
常见错误与解决办法
在 Server 模式下留意 Output 窗口。
按钮一直显示 Loading...
玩家的记录没有加载出来。在 Studio 中开启 Enable Studio Access to API Services。Output 中的错误显示在“could not load”之后。
领取时显示“Come back later”,可按钮上明明是 Claim
保存的记录比按钮知道的更新,例如玩家刚在另一个服务器上领取过。以服务端给出的回答为准。
连续天数每天都被重置
玩家两次领取的间隔超过 48 小时,或者 STREAK_LOST_AFTER 还停留在测试值。
金币没有增加
玩家的 leaderstats 中必须有一个由服务端脚本创建的 IntValue Coins。
用 RoCode 构建
如果不想手动设置这一切,RoCode 可以在你打开的场景里把它搭好。RoCode 是独立的 Roblox Studio AI 代理,并非 Roblox 出品:你在网页聊天里描述改动,它的 Studio 插件就会在你的 Explorer 中创建脚本。
添加 7 天连续签到的每日奖励:第 1 到第 6 天给金币,第 7 天给一颗宝石,加一个带倒计时的按钮,数据要保存并在服务端检查。
- 可以一步插入它高级 UI 套件中现成的动画每日奖励界面,有五种主题(candy、neon、gold、blocky、pastel)可选,并带有可用的服务端脚本
- 或者编写本教程中的服务端脚本和按钮,并接到你已有的货币上
- 它被要求用 Check Script 检查每个脚本能否编译,并在每批改动前后设置撤销点
搭建完成后,RoCode 可以运行它的测试机器人:机器人在 Studio 里玩你的游戏,点击按钮,并报告 PASS 或 FAIL。它只检查自己选择测试的内容,而 24 小时的计时器需要你自己用较短的值来测试。RoCode 如何连接到 Studio。
常见问题
Roblox 怎么做每日奖励?
在服务端用 os.time() 保存每个玩家上次领取的时间,24 小时后允许领取,并在发放奖励之前在 UpdateAsync 内部保存。脚本见第 1 步。
怎么做连续登录奖励?
把连续天数计数和领取时间一起保存。如果下一次领取在 48 小时以内,就加一;如果更晚,就从 1 重新开始。
玩家能通过调时钟来骗取每日奖励吗?
用这个脚本不能:它只使用服务端的 os.time()。
是不是在午夜重置奖励更好?
可以改为按 UTC 的日历日来计算,而不是 24 小时。滚动的 24 小时更简单,对所有时区的效果都一样。
参考资料
- Roblox Creator Docs: GlobalDataStore:UpdateAsync(英文). 更新函数以及通过 nil 取消
- Roblox Creator Docs: Data stores(英文). Studio 访问和 data store 基础
- Roblox Creator Docs: Remote events and functions(英文). RemoteFunction
代码如何验证:本页的所有脚本都已用 Roblox API 类型定义(luau-lsp,2026年11月20日)通过 Luau strict 类型检查。类型检查能发现 API 名称写错和类型不匹配,但发现不了游戏逻辑上的错误。发布前请务必在 Studio 中测试。