{"id":"nahidwinbread/stat-modifier","name":"stat-modifier","scope":"nahidwinbread","platform":"roblox","description":"A simple stat modifire class","version":"0.1.0","latest":"0.1.0","versions":["0.1.0"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{},"integrity":"f0e4a5d92b8d50617285b2c54ec3b6f2a5654d3ea7646405e74f8a0ea5876ed3","likes":0,"downloads":0,"install":"forest install nahidwinbread/stat-modifier","url":"https://forest.dev/p/roblox/nahidwinbread/stat-modifier","files":"https://api.forest.dev/ai/package/roblox/nahidwinbread/stat-modifier/files","readme":"# StatModifier\n\nStatModifier is a lightweight, dependency-free stat management library for Roblox projects written in Luau.\n\nIt supports additive and multiplicative modifiers, priority-based overrides, minimum and maximum limits, tags, timed sources, pause/resume behavior, and change events.\n\n## Features\n\n- Multiple named stats with configurable base values\n- `Add`, `Mul`, and `Override` modifiers\n- Priority and application-order handling for overrides\n- Minimum and maximum (clamped) values\n- Timed modifiers with refresh and extension support\n- Pausable modifiers that preserve remaining duration\n- Source lookup by tag\n- `Changed`, `Applied`, and `Removed` events\n- Shared heartbeat scheduling for timed sources\n- Strict runtime validation\n\n## Installation\n\n### Wally\n\nOnce this repository is published as a Wally package, add the package to `wally.toml` using the package owner's Wally name:\n\n```toml\n[dependencies]\nStatModifier = \"owner/statModifier@<version>\"\n```\n\nReplace `owner` and `<version>` with the published package values, then run:\n\n```bash\nwally install\n```\n\n## Quick Start\n\n```lua\nlocal ReplicatedStorage = game:GetService(\"ReplicatedStorage\")\nlocal StatModifier = require(ReplicatedStorage.Packages.StatModifier)\n\nlocal stats = StatModifier.new({\n\tAttack = { base = 10, min = 0, max = 999 },\n\tWalkSpeed = 16,\n})\n\nstats.Changed:Connect(function(stat, newValue, oldValue)\n\tprint(stat, oldValue, \"->\", newValue)\nend)\n\nstats:Apply(\"Rage\", {\n\t{ stat = \"Attack\", kind = \"Add\", value = 5 },\n\t{ stat = \"Attack\", kind = \"Mul\", value = 1.5 },\n}, {\n\tduration = 10,\n\tpriority = 0,\n\ttags = { \"Buff\", \"Combat\" },\n})\n\nprint(stats:Get(\"Attack\")) -- 22.5\nprint(stats:GetRemaining(\"Rage\")) -- seconds remaining\n\nstats:Destroy()\n```\n\n## Calculation\n\nA stat is calculated as follows:\n\n1. If an active `Override` modifier exists, use the winning override value.\n2. Otherwise, calculate `(base + sum of Add values) * product of Mul values`.\n3. Clamp the result to the configured `min` and `max`, if present.\n\nOverride winners are resolved by the highest `priority`. If priorities are equal, the most recently applied source wins.\n\n```lua\n-- (100 + 20 + 30) * 1.5 * 1.2 = 270\nlocal result = StatModifier.new({\n\tAttack = { base = 100, max = 200 },\n})\n\nresult:Apply(\"A\", { { stat = \"Attack\", kind = \"Add\", value = 20 } })\nresult:Apply(\"B\", { { stat = \"Attack\", kind = \"Add\", value = 30 } })\nresult:Apply(\"C\", { { stat = \"Attack\", kind = \"Mul\", value = 1.5 } })\nresult:Apply(\"D\", { { stat = \"Attack\", kind = \"Mul\", value = 1.2 } })\n\nprint(result:Get(\"Attack\")) -- 200 (clamped)\n```\n\n`Pause` excludes a source's `Add`, `Mul`, and `Override` entries from calculations and freezes its remaining duration.\n\n## Public API\n\n### Constructor\n\n```lua\nStatModifier.new(configs)\n```\n\nEach config is either a number or a table:\n\n```lua\nlocal stats = StatModifier.new({\n\tHealth = { base = 100, min = 0, max = 100 },\n\tAttack = 10,\n})\n```\n\n### Reading Values\n\n| Method | Returns | Description |\n| --- | --- | --- |\n| `Get(stat)` | `number` | Final stat value |\n| `GetBase(stat)` | `number` | Base value |\n| `GetAddTotal(stat)` | `number` | Sum of active `Add` values |\n| `GetMulTotal(stat)` | `number` | Product of active `Mul` values |\n| `Has(sourceId)` | `boolean` | Whether a source exists |\n| `IsPaused(sourceId)` | `boolean` | Whether a source is paused |\n| `GetRemaining(sourceId)` | `number?` | Remaining seconds, frozen while paused |\n| `GetSourceIdsByTag(tag)` | `{ string }` | Source IDs associated with a tag |\n\n### Applying and Removing Sources\n\n| Method | Description |\n| --- | --- |\n| `Apply(sourceId, entries, options?)` | Applies or replaces a source |\n| `Remove(sourceId)` | Removes a source |\n| `Clear()` | Removes all sources |\n| `SetBase(stat, value)` | Changes a stat's base value |\n\nApply options:\n\n```lua\n{\n\tduration = 10, -- number of seconds; omitted means permanent\n\tpriority = 0,  -- override priority\n\ttags = { \"Buff\", \"Combat\" },\n\tpaused = false,\n}\n```\n\nA source ID is unique within a container. Applying the same ID replaces the previous source, including its entries, duration, priority, tags, and pause state.\n\n### Timing and Pause\n\n| Method | Returns | Description |\n| --- | --- | --- |\n| `Pause(sourceId)` | `boolean` | Pauses a source and freezes its timer |\n| `Resume(sourceId)` | `boolean` | Resumes a paused source |\n| `Refresh(sourceId, duration?)` | `boolean` | Resets the timer |\n| `Extend(sourceId, seconds)` | `boolean` | Adds or subtracts time |\n\n## Events\n\n```lua\nstats.Changed:Connect(function(stat, newValue, oldValue)\n\t-- Fired only when the final value changes\nend)\n\nstats.Applied:Connect(function(sourceId, tags)\n\t-- Fired after Apply\nend)\n\nstats.Removed:Connect(function(sourceId, reason, tags)\n\t-- reason: \"Manual\", \"Expired\", \"Replaced\", or \"Cleared\"\nend)\n```\n\n`Changed` fires once per changed stat per mutation. Event handlers are called after state has been updated.\n\n## Validation\n\nInvalid values and unknown stats raise a Luau error. Invalid input includes:\n\n- Empty source or stat names\n- `NaN` or non-number values\n- Empty modifier entry arrays\n- Non-positive durations\n- Invalid tags\n- A minimum greater than a maximum\n\n## Testing\n\nThe test files use Luau `assert` checks and the repository's local test configuration.\n\n```bash\nrojo build dev.project.json -o StatModifierTests.rbxlx\n```\n\nOpen `StatModifierTests.rbxlx` in Roblox Studio and start Play mode to run the test scripts under `ServerScriptService`.\n\nTimed-source tests replace `Scheduler._deps` with a deterministic clock and heartbeat test connection.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n","readmeTruncated":false}