Start typing to search packages!
stat-modifier
By @nahidwinbread
Roblox
Mirrored from WallyStatModifier
StatModifier is a lightweight, dependency-free stat management library for Roblox projects written in Luau.
It supports additive and multiplicative modifiers, priority-based overrides, minimum and maximum limits, tags, timed sources, pause/resume behavior, and change events.
Features
- Multiple named stats with configurable base values
Add,Mul, andOverridemodifiers- Priority and application-order handling for overrides
- Minimum and maximum (clamped) values
- Timed modifiers with refresh and extension support
- Pausable modifiers that preserve remaining duration
- Source lookup by tag
Changed,Applied, andRemovedevents- Shared heartbeat scheduling for timed sources
- Strict runtime validation
Installation
Wally
Once this repository is published as a Wally package, add the package to wally.toml using the package owner's Wally name:
[dependencies]
StatModifier = "owner/statModifier@<version>"
Replace owner and <version> with the published package values, then run:
wally install
Quick Start
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StatModifier = require(ReplicatedStorage.Packages.StatModifier)
local stats = StatModifier.new({
Attack = { base = 10, min = 0, max = 999 },
WalkSpeed = 16,
})
stats.Changed:Connect(function(stat, newValue, oldValue)
print(stat, oldValue, "->", newValue)
end)
stats:Apply("Rage", {
{ stat = "Attack", kind = "Add", value = 5 },
{ stat = "Attack", kind = "Mul", value = 1.5 },
}, {
duration = 10,
priority = 0,
tags = { "Buff", "Combat" },
})
print(stats:Get("Attack")) -- 22.5
print(stats:GetRemaining("Rage")) -- seconds remaining
stats:Destroy()
Calculation
A stat is calculated as follows:
- If an active
Overridemodifier exists, use the winning override value. - Otherwise, calculate
(base + sum of Add values) * product of Mul values. - Clamp the result to the configured
minandmax, if present.
Override winners are resolved by the highest priority. If priorities are equal, the most recently applied source wins.
-- (100 + 20 + 30) * 1.5 * 1.2 = 270
local result = StatModifier.new({
Attack = { base = 100, max = 200 },
})
result:Apply("A", { { stat = "Attack", kind = "Add", value = 20 } })
result:Apply("B", { { stat = "Attack", kind = "Add", value = 30 } })
result:Apply("C", { { stat = "Attack", kind = "Mul", value = 1.5 } })
result:Apply("D", { { stat = "Attack", kind = "Mul", value = 1.2 } })
print(result:Get("Attack")) -- 200 (clamped)
Pause excludes a source's Add, Mul, and Override entries from calculations and freezes its remaining duration.
Public API
Constructor
StatModifier.new(configs)
Each config is either a number or a table:
local stats = StatModifier.new({
Health = { base = 100, min = 0, max = 100 },
Attack = 10,
})
Reading Values
| Method | Returns | Description |
|---|---|---|
Get(stat) | number | Final stat value |
GetBase(stat) | number | Base value |
GetAddTotal(stat) | number | Sum of active Add values |
GetMulTotal(stat) | number | Product of active Mul values |
Has(sourceId) | boolean | Whether a source exists |
IsPaused(sourceId) | boolean | Whether a source is paused |
GetRemaining(sourceId) | number? | Remaining seconds, frozen while paused |
GetSourceIdsByTag(tag) | { string } | Source IDs associated with a tag |
Applying and Removing Sources
| Method | Description |
|---|---|
Apply(sourceId, entries, options?) | Applies or replaces a source |
Remove(sourceId) | Removes a source |
Clear() | Removes all sources |
SetBase(stat, value) | Changes a stat's base value |
Apply options:
{
duration = 10, -- number of seconds; omitted means permanent
priority = 0, -- override priority
tags = { "Buff", "Combat" },
paused = false,
}
A source ID is unique within a container. Applying the same ID replaces the previous source, including its entries, duration, priority, tags, and pause state.
Timing and Pause
| Method | Returns | Description |
|---|---|---|
Pause(sourceId) | boolean | Pauses a source and freezes its timer |
Resume(sourceId) | boolean | Resumes a paused source |
Refresh(sourceId, duration?) | boolean | Resets the timer |
Extend(sourceId, seconds) | boolean | Adds or subtracts time |
Events
stats.Changed:Connect(function(stat, newValue, oldValue)
-- Fired only when the final value changes
end)
stats.Applied:Connect(function(sourceId, tags)
-- Fired after Apply
end)
stats.Removed:Connect(function(sourceId, reason, tags)
-- reason: "Manual", "Expired", "Replaced", or "Cleared"
end)
Changed fires once per changed stat per mutation. Event handlers are called after state has been updated.
Validation
Invalid values and unknown stats raise a Luau error. Invalid input includes:
- Empty source or stat names
NaNor non-number values- Empty modifier entry arrays
- Non-positive durations
- Invalid tags
- A minimum greater than a maximum
Testing
The test files use Luau assert checks and the repository's local test configuration.
rojo build dev.project.json -o StatModifierTests.rbxlx
Open StatModifierTests.rbxlx in Roblox Studio and start Play mode to run the test scripts under ServerScriptService.
Timed-source tests replace Scheduler._deps with a deterministic clock and heartbeat test connection.
License
This project is licensed under the MIT License.
Package Details
Install command (Click to copy)
Version
0.1.0
License
MIT
Safe for commercial use
Automated license review — not legal advice.
