What this script does
Sort validated pet records by power and id, select up to the requested slot count, and calculate the resulting total power.
- Best for
- Prototype “equip best” behavior in an experience you own, with a score your server controls.
- 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
- Stable score sorting
- Slot-limited selection
- Total-power helper
Install in the stated runtime
- 1Create a ModuleScript named PetLoadout.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Pass server-owned records and treat power as your project’s documented score, not a copied live-game value.
Minimal usage example
local PetLoadout = require(script.Parent.PetLoadout)
local selected = PetLoadout.selectBest({
{ id = "a", power = 15 }, { id = "b", power = 30 }
}, 1)
print(selected[1].id)Example inputs and result
- Supply
- Pet records with ids and project-defined power scores, plus a slot limit.
- Expect
- selectBest returns the highest-scoring records without changing the supplied table.
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. Selects a best-scoring loadout from records you supply.
export type PetRecord = { id: string, power: number, locked: boolean? }
local PetLoadout = {}
function PetLoadout.selectBest(pets: { PetRecord }, slots: number): { PetRecord }
local candidates: { PetRecord } = {}
for _, pet in pets do
if pet.id ~= "" and pet.power >= 0 then table.insert(candidates, table.clone(pet)) end
end
table.sort(candidates, function(a: PetRecord, b: PetRecord)
if a.power == b.power then return a.id < b.id end
return a.power > b.power
end)
local selected: { PetRecord } = {}
for index = 1, math.min(math.max(0, math.floor(slots)), #candidates) do
table.insert(selected, candidates[index])
end
return selected
end
function PetLoadout.totalPower(pets: { PetRecord }): number
local total = 0
for _, pet in pets do total += math.max(0, pet.power) end
return total
end
return table.freeze(PetLoadout)
ac1a27c27da69387fd1656a4855245663a229ed8ec69adb648738658cffa55ddOutside this script’s scope
- Inspect another game’s pets
- Equip anything in Steal An Egg
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.