{"id":"zaidenmtz1/offline-earnings","name":"offline-earnings","scope":"zaidenmtz1","platform":"roblox","description":"Offline earnings for Roblox idle games: caps, efficiency, boosts that expire mid-absence, and clock safety.","version":"0.1.0","latest":"0.1.0","versions":["0.1.0"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{},"integrity":"e67590f278c537c477a7cd423edccabb2ac58da822219ff3efb22cdb525c4262","likes":0,"downloads":0,"install":"forest install zaidenmtz1/offline-earnings","url":"https://forest.dev/p/roblox/zaidenmtz1/offline-earnings","files":"https://api.forest.dev/ai/package/roblox/zaidenmtz1/offline-earnings/files","readme":"# OfflineEarnings\n\nOffline earnings for Roblox idle, tycoon, and simulator games.\n\nGive it when the player left, how fast they earn, and a few rules. It tells you what they earned while away, plus a ready-to-show \"2h 45m\" string for your Welcome Back popup.\n\nIt's a **pure calculation module**: no DataStore code, no Roblox services, no UI. You keep using whatever saving system your game already has (ProfileService, raw DataStore, anything).\n\n## Why use this instead of `os.time() - lastSeen`?\n\nThe basic snippet works until it doesn't. This module handles the parts that usually get rebuilt (or broken) game by game:\n\n- **Cap**: limit offline time (default 8 hours).\n- **Efficiency**: earn only a fraction of the online rate while away.\n- **Boosts that expire mid-absence**: a 2x boost that ended 1 hour into a 5-hour absence only counts for that first hour.\n- **Boosts that start mid-absence**, and overlapping boosts (they multiply).\n- **Clock safety**: a missing, future, or corrupted `lastSeen` returns zero instead of free money or a crash.\n- **Minimum threshold**: ignore tiny absences so quick rejoins don't trigger a popup.\n- **Duration formatting** for your UI.\n\n## Install\n\n**Wally** (once published; replace `zaidenmtz1` with the real scope):\n\n```toml\n[dependencies]\nOfflineEarnings = \"zaidenmtz1/offline-earnings@0.1.0\"\n```\n\n**Manual:** copy `src/init.luau` into a ModuleScript named `OfflineEarnings` in `ReplicatedStorage` or `ServerScriptService`.\n\n## Quick start\n\n```lua\nlocal OfflineEarnings = require(path.to.OfflineEarnings)\n\nlocal result = OfflineEarnings.calculate({\n\tlastSeen = data.LastSeen,          -- os.time() you saved when they left (nil for new players)\n\tnow = os.time(),                   -- server time only\n\tratePerSecond = data.CoinsPerSecond,\n\tmaxSeconds = 8 * 3600,             -- count at most 8 hours away\n\tefficiency = 0.5,                  -- earn 50% of the online rate while away\n\tminSeconds = 60,                   -- ignore absences under a minute\n\tmodifiers = {\n\t\t-- a 2x boost that was active when they left and ends at data.BoostEndsAt\n\t\t{ multiplier = 2, expiresAt = data.BoostEndsAt },\n\t},\n})\n\nif result.earned > 0 then\n\tlocal coins = math.floor(result.earned)\n\tdata.Coins += coins\n\tprint(`Welcome back! You were gone {OfflineEarnings.formatDuration(result.secondsAway)} and earned {coins} coins.`)\n\tif result.wasCapped then\n\t\tprint(\"(Offline earnings are capped at 8 hours.)\")\n\tend\nend\n```\n\n### Where `lastSeen` comes from\n\nSave `os.time()` into the player's data when they leave **and** whenever you autosave:\n\n```lua\nPlayers.PlayerRemoving:Connect(function(player)\n\tlocal data = getData(player)\n\tdata.LastSeen = os.time()\n\t-- ...save as usual\nend)\n```\n\nIf a server crashes and `LastSeen` is stale, the player would be credited for time they were actually online. Updating `LastSeen` on every autosave keeps that gap small.\n\n## API\n\n### `OfflineEarnings.calculate(config): Result`\n\n| Field | Type | Default | Meaning |\n|---|---|---|---|\n| `lastSeen` | `number?` | none | Unix time saved when the player left. `nil` means a new player. |\n| `now` | `number` | required | Current unix time. Use `os.time()` on the **server**. |\n| `ratePerSecond` | `number` | required | Earnings per second while online, before boosts and efficiency. |\n| `maxSeconds` | `number?` | `28800` (8h) | Maximum offline seconds that count. `math.huge` for no cap. |\n| `efficiency` | `number?` | `1` | Fraction of the online rate earned while away. |\n| `minSeconds` | `number?` | `0` | Absences shorter than this earn nothing. |\n| `modifiers` | `{Modifier}?` | none | Boosts that were active around the time the player left. |\n\n**Modifier:** `{ multiplier: number, startsAt: number?, expiresAt: number? }`. Omit `startsAt` if it was already active; omit `expiresAt` if it never ends. Overlapping modifiers multiply. A `multiplier` of `0` pauses earnings while it's active.\n\n**Result:**\n\n| Field | Meaning |\n|---|---|\n| `earned` | Total earned. **Not rounded**: use `math.floor` or `math.round` yourself. |\n| `secondsAway` | Real time the player was away (0 if it couldn't be determined). |\n| `secondsCounted` | Seconds that counted after the cap. |\n| `wasCapped` | `true` if the absence was longer than `maxSeconds`. |\n| `reason` | `\"ok\"`, `\"no_last_seen\"`, `\"clock_skew\"` (lastSeen in the future), or `\"below_minimum\"`. |\n\nBad **data** (missing or future `lastSeen`) returns a zero result with a `reason`. Bad **arguments** (negative rate, non-number `now`, ...) raise an error so bugs get caught in development.\n\n### `OfflineEarnings.formatDuration(seconds, maxParts?): string`\n\n```lua\nOfflineEarnings.formatDuration(9900)        --> \"2h 45m\"\nOfflineEarnings.formatDuration(90061)       --> \"1d 1h\"\nOfflineEarnings.formatDuration(90061, 3)    --> \"1d 1h 1m\"\nOfflineEarnings.formatDuration(45)          --> \"45s\"\n```\n\n## How boosts and the cap interact\n\nEverything is measured from the moment the player left. The cap trims the **end** of the absence, and boosts are only counted inside the part that remains. Efficiency is applied last.\n\nExample: away 12 hours, cap 8 hours, a 2x boost that ends 3 hours after they left → hours 0-3 earn at 2x, hours 3-8 at 1x, hours 8-12 earn nothing.\n\n## What it doesn't do (yet)\n\n- **Saving or loading data.** Bring your own DataStore or ProfileService setup.\n- **Big-number support.** It uses regular Luau numbers. Games that exceed ~1e308 need a big-number library and their own conversion.\n- **Changing rates over time.** It assumes `ratePerSecond` was constant while away (boosts aside).\n- **Exploit protection.** It trusts the numbers you give it. Always pass `os.time()` from the server and never accept timestamps from the client.\n- **UI.** It gives you the numbers and a formatted duration; the popup is yours.\n\n## Tests\n\nThe test suite needs no framework, only the [Luau CLI](https://github.com/luau-lang/luau/releases) or [Lune](https://github.com/lune-org/lune):\n\n```bash\nluau tests/OfflineEarnings.spec.luau\n```\n\n## Contributing\n\nIssues and PRs are welcome, especially edge cases you hit in a real game.\n\n## License\n\nMIT. See [LICENSE](LICENSE).\n","readmeTruncated":false}