What this script does
Collect named counters and elapsed time without analytics services, network requests, or assumptions about your gameplay model.
- Best for
- Balancing and playtest summaries inside a Studio project.
- 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
- Any named numeric counter
- Elapsed session time
- Snapshot and reset helpers
Install in the stated runtime
- 1Create a ModuleScript named SessionStats.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Create one tracker for a controlled test and decide yourself whether its snapshot should be displayed or saved.
Minimal usage example
local SessionStats = require(script.Parent.SessionStats)
local stats = SessionStats.new()
stats:add("EggsCollected")
stats:add("MoneyEarned", 250)
print(stats:snapshot().counters.MoneyEarned)Example inputs and result
- Supply
- Counter names and increments from your own session events.
- Expect
- snapshot returns the current in-memory counters for reporting or testing.
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. In-memory session metrics for your own Studio project.
export type SessionStats = {
startedAt: number,
counters: { [string]: number },
add: (self: SessionStats, name: string, amount: number?) -> (),
get: (self: SessionStats, name: string) -> number,
snapshot: (self: SessionStats, now: number?) -> { elapsedSeconds: number, counters: { [string]: number } },
reset: (self: SessionStats, now: number?) -> (),
}
local SessionStats = {}
SessionStats.__index = SessionStats
function SessionStats.new(startedAt: number?): SessionStats
return setmetatable({ startedAt = startedAt or os.clock(), counters = {} :: { [string]: number } }, SessionStats) :: any
end
function SessionStats.add(self: SessionStats, name: string, amount: number?)
assert(name ~= "", "Counter name cannot be empty")
self.counters[name] = (self.counters[name] or 0) + (amount or 1)
end
function SessionStats.get(self: SessionStats, name: string): number return self.counters[name] or 0 end
function SessionStats.snapshot(self: SessionStats, now: number?): { elapsedSeconds: number, counters: { [string]: number } }
return { elapsedSeconds = math.max(0, (now or os.clock()) - self.startedAt), counters = table.clone(self.counters) }
end
function SessionStats.reset(self: SessionStats, now: number?)
self.startedAt = now or os.clock()
table.clear(self.counters)
end
return SessionStats
617e285d9844f983c1fafb6dfe6ce477e89f0964df754bec130d1acc822b222cOutside this script’s scope
- Upload analytics
- Identify or track players across sessions
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.