What this script does
Compare project-owned pet or producer upgrades without purchases, client automation, or a hard-coded live-game value list.
- Best for
- Balancing pet replacements, generators, base upgrades, and purchase previews in your own economy prototype.
- 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
- Added-income calculation
- Payback and session gain
- Budget-aware best candidate
Install in the stated runtime
- 1Create a ModuleScript named PetUpgrade.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Use trusted server values and revalidate the selected candidate before your own purchase system changes player data.
Minimal usage example
local PetUpgrade = require(script.Parent.PetUpgrade)
local result = PetUpgrade.evaluate(100, {
id = "fox-upgrade", cost = 5_000, newIncomePerSecond = 140,
})
print(result.addedIncomePerSecond, result.paybackSeconds)Example inputs and result
- Supply
- Current income rate and an upgrade with cost and candidate new rate.
- Expect
- evaluate returns added income and estimated payback time for those assumptions.
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 comparison math for values owned by your Studio project.
export type UpgradeCandidate = { id: string, cost: number, newIncomePerSecond: number }
export type UpgradeResult = {
id: string,
cost: number,
addedIncomePerSecond: number,
paybackSeconds: number?,
}
local PetUpgrade = {}
function PetUpgrade.evaluate(currentIncomePerSecond: number, candidate: UpgradeCandidate): UpgradeResult
assert(currentIncomePerSecond >= 0, "Current income must be non-negative")
assert(candidate.id ~= "" and candidate.cost >= 0 and candidate.newIncomePerSecond >= 0, "Invalid candidate")
local added = candidate.newIncomePerSecond - currentIncomePerSecond
return {
id = candidate.id,
cost = candidate.cost,
addedIncomePerSecond = added,
paybackSeconds = if added > 0 then candidate.cost / added else nil,
}
end
function PetUpgrade.best(currentIncomePerSecond: number, candidates: { UpgradeCandidate }, budget: number?): UpgradeResult?
local bestResult: UpgradeResult? = nil
local limit = budget or math.huge
assert(limit >= 0, "Budget must be non-negative")
for _, candidate in candidates do
local result = PetUpgrade.evaluate(currentIncomePerSecond, candidate)
if result.cost <= limit and result.paybackSeconds ~= nil then
if bestResult == nil or (result.paybackSeconds :: number) < (bestResult.paybackSeconds :: number) then
bestResult = result
end
end
end
return bestResult
end
function PetUpgrade.sessionGain(result: UpgradeResult, sessionSeconds: number): number
assert(sessionSeconds >= 0, "Session length must be non-negative")
return result.addedIncomePerSecond * sessionSeconds - result.cost
end
return table.freeze(PetUpgrade)
0797dc02173ddf124bf7a134e47c4412a4bc1e3a7163604a0d629fcc882833a8Outside this script’s scope
- Buy, equip, sell, or replace a live pet
- Read another game’s prices or income
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.