What this script does
Advance a balance from a rate and elapsed time. The module leaves saving, UI, and reward ownership to your own server code.
- Best for
- Prototype passive income or pet economy math in your own Roblox Studio experience.
- 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
- Frame-rate-independent income calculation
- Change the rate without replacing the counter
- Reset to a known total for repeatable tests
- No DataStore or networking assumptions
Install in the stated runtime
- 1Create a ModuleScript named IncomeCounter.
- 2Paste the source and require it from a server Script.
- 3Call step with a trusted server-side delta and save the resulting total through your own data layer.
Minimal usage example
local RunService = game:GetService("RunService")
local IncomeCounter = require(script.Parent.IncomeCounter)
local counter = IncomeCounter.new(25, 0)
RunService.Heartbeat:Connect(function(deltaSeconds)
counter:step(deltaSeconds)
end)Example inputs and result
- Supply
- A server-owned income rate, starting balance, and elapsed seconds on each step.
- Expect
- The counter advances its local balance; saving and player ownership remain your responsibility.
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. For Roblox Studio experiences you own or can edit.
-- Deterministic income math only; connect it to your own server-side data model.
export type IncomeCounter = {
ratePerSecond: number,
total: number,
step: (self: IncomeCounter, deltaSeconds: number) -> number,
setRate: (self: IncomeCounter, ratePerSecond: number) -> (),
reset: (self: IncomeCounter, total: number?) -> (),
}
local IncomeCounter = {}
IncomeCounter.__index = IncomeCounter
function IncomeCounter.new(ratePerSecond: number, startingTotal: number?): IncomeCounter
assert(ratePerSecond >= 0, "Rate must be zero or greater")
local self = setmetatable({
ratePerSecond = ratePerSecond,
total = math.max(0, startingTotal or 0),
}, IncomeCounter)
return self :: any
end
function IncomeCounter.step(self: IncomeCounter, deltaSeconds: number): number
assert(deltaSeconds >= 0, "Delta time must be zero or greater")
local earned = self.ratePerSecond * deltaSeconds
self.total += earned
return earned
end
function IncomeCounter.setRate(self: IncomeCounter, ratePerSecond: number)
assert(ratePerSecond >= 0, "Rate must be zero or greater")
self.ratePerSecond = ratePerSecond
end
function IncomeCounter.reset(self: IncomeCounter, total: number?)
self.total = math.max(0, total or 0)
end
return IncomeCounter
6c807fe293d3d2e46fdbb9af635710b5009d39f98956c088b478cbd7d11c6300Outside this script’s scope
- Claim currency in another experience
- Read another game’s pets or balance
- Send data outside your Studio project
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.