What this script does
Start a whole-second countdown without embedding UI or round-state assumptions inside the timer.
- Best for
- Round systems, intermissions, lobby starts, timed routes, and event prototypes.
- Language
- Luau
- Version
- 1.0.0
- Validation
- Luau checked · Studio integration required
Official Luau 0.735 parsing and static analysis pass with Roblox host types stubbed. Test the actual Instance, service, or event integration in a separate Studio place before production use.
Included behavior
- Per-second tick callback
- Completion callback
- Returned cancellation function
Install in the stated runtime
- 1Create a ModuleScript named RoundCountdown.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Keep the returned cancel function and call it whenever the round state changes or the owning object is removed.
Minimal usage example
local Countdown = require(script.Parent.RoundCountdown)
local cancel = Countdown.start(10, function(left)
print(left)
end, function()
print("Round starts")
end)Example inputs and result
- Supply
- Duration in seconds, a tick callback, and a completion callback.
- Expect
- start returns a cancellation function for that countdown.
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. Cancelable countdown callbacks for your own Studio round loop.
local RoundCountdown = {}
function RoundCountdown.start(seconds: number, onTick: (number) -> (), onComplete: () -> ())
assert(seconds >= 0, "Seconds must be non-negative")
local state = { cancelled = false }
task.spawn(function()
for remaining = math.floor(seconds), 1, -1 do
if state.cancelled then return end
onTick(remaining)
task.wait(1)
end
if not state.cancelled then onTick(0); onComplete() end
end)
return function() state.cancelled = true end
end
return table.freeze(RoundCountdown)
99968cfb99d58fef47658c77f5c1d9e1aff1512f7c896ebd91509fced9cbcbb0Outside this script’s scope
- Manage teams or rewards
- Persist a countdown between servers
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.