What this script does
Test an original upgrade curve without shop Instances or purchases. A hard iteration limit prevents accidental infinite affordability loops.
- Best for
- Balancing base, capacity, treadmill, or production upgrades in your own 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
- Geometric per-level cost
- Total cost across a level range
- Maximum affordable level count
Install in the stated runtime
- 1Create a ModuleScript named UpgradeCost.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Choose your own base cost and growth factor, then display results before any server-side purchase validation.
Minimal usage example
local Cost = require(script.Parent.UpgradeCost)
print(Cost.geometric(100, 1.35, 5))
print(Cost.total(100, 1.35, 0, 10))
print(Cost.maxAffordable(5_000, 100, 1.35, 0))Example inputs and result
- Supply
- Your base cost, growth factor, levels, and optional budget.
- Expect
- The helpers return a level price, a range total, or an affordable level count.
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 upgrade-cost math for your own balancing spreadsheet or UI.
local UpgradeCost = {}
function UpgradeCost.geometric(baseCost: number, growth: number, level: number): number
assert(baseCost >= 0 and growth >= 1 and level >= 0, "Invalid cost curve inputs")
return math.floor(baseCost * growth ^ math.floor(level) + 0.5)
end
function UpgradeCost.total(baseCost: number, growth: number, startLevel: number, levels: number): number
local sum = 0
for level = startLevel, startLevel + math.max(0, math.floor(levels)) - 1 do
sum += UpgradeCost.geometric(baseCost, growth, level)
end
return sum
end
function UpgradeCost.maxAffordable(balance: number, baseCost: number, growth: number, startLevel: number): number
local bought, spent = 0, 0
while bought < 10_000 do
local cost = UpgradeCost.geometric(baseCost, growth, startLevel + bought)
if spent + cost > balance then break end
spent += cost
bought += 1
end
return bought
end
return table.freeze(UpgradeCost)
d04c742875365d9fdbac8cafff4ce831bd3ad7785d1f2a8d40ff5b5c8c34b171Outside this script’s scope
- Buy upgrades
- Use or predict Steal An Egg prices
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.