Forest Logo
search
package_2

stat-modifier

By @nahidwinbread

Roblox

Mirrored from Wally

StatModifier

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, and Override modifiers
  • 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, and Removed events
  • 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:

  1. If an active Override modifier exists, use the winning override value.
  2. Otherwise, calculate (base + sum of Add values) * product of Mul values.
  3. Clamp the result to the configured min and max, 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

MethodReturnsDescription
Get(stat)numberFinal stat value
GetBase(stat)numberBase value
GetAddTotal(stat)numberSum of active Add values
GetMulTotal(stat)numberProduct of active Mul values
Has(sourceId)booleanWhether a source exists
IsPaused(sourceId)booleanWhether 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

MethodDescription
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

MethodReturnsDescription
Pause(sourceId)booleanPauses a source and freezes its timer
Resume(sourceId)booleanResumes a paused source
Refresh(sourceId, duration?)booleanResets the timer
Extend(sourceId, seconds)booleanAdds 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
  • NaN or 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

check_circle

Safe for commercial use

Automated license review — not legal advice.