What this script does
Keep daily reward timing separate from UI and saving. Tests can inject a fixed current time for repeatable results.
- Best for
- Daily rewards, group rewards, and other server-authoritative cooldown prototypes.
- Language
- Luau
- Version
- 1.0.0
- Validation
- Luau runtime tested
Official Luau 0.735 parsing and strict analysis pass, and the module is covered by the pure-module runtime behavior harness.
Included behavior
- Claim eligibility check
- Remaining and next-claim timestamps
- Injectable clock for deterministic tests
Install in the stated runtime
- 1Create a ModuleScript named DailyReward.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Store the accepted claim timestamp server-side and validate it again before awarding your own reward.
Minimal usage example
local DailyReward = require(script.Parent.DailyReward)
local allowed, claimedAt = DailyReward.claim(lastClaimAt, 24 * 60 * 60)
if allowed then print("Reward accepted at", claimedAt) endExample inputs and result
- Supply
- Previous claim time and cooldown duration in seconds.
- Expect
- claim returns whether a new claim is allowed and the accepted timestamp.
These notes explain the example above. Read the complete source for every exported function and edge case before integrating it.
Complete source
No hidden import, minified block, or shortened loader.
--!strict
-- MIT License. Pure timestamp helpers for a reward system in your own experience.
local DailyReward = {}
function DailyReward.nextClaimAt(lastClaimAt: number?, cooldownSeconds: number): number
assert(cooldownSeconds >= 0, "Cooldown must be non-negative")
return math.max(0, lastClaimAt or 0) + cooldownSeconds
end
function DailyReward.remaining(lastClaimAt: number?, cooldownSeconds: number, now: number?): number
return math.max(0, DailyReward.nextClaimAt(lastClaimAt, cooldownSeconds) - (now or os.time()))
end
function DailyReward.canClaim(lastClaimAt: number?, cooldownSeconds: number, now: number?): boolean
return DailyReward.remaining(lastClaimAt, cooldownSeconds, now) == 0
end
function DailyReward.claim(lastClaimAt: number?, cooldownSeconds: number, now: number?): (boolean, number)
local currentTime = now or os.time()
if not DailyReward.canClaim(lastClaimAt, cooldownSeconds, currentTime) then return false, lastClaimAt or 0 end
return true, currentTime
end
return table.freeze(DailyReward)
f6d3ef9d7288809690650f0fd4e20d0a57938d05ce24df206ec42543edeecc28Outside this script’s scope
- Grant items or currency
- Persist the last claim automatically
Found a problem or a better approach?
Tell us what failed, what you expected, and whether the issue happened in the Luau CLI or Roblox Studio. Suggestions for clearer types, safer server checks, or a more useful API are welcome.