Boost
By @avodey (my main account)!
Module
This uses an Assert module, and Event module, both of which I made not specifically for this. It would help if you found bugs in those!
Summary (v1.0.1)
Ever wanted to easily add some sort of value modifiers? You know, like these?
An amulet which boosts certain game aspects, in Bee Swarm Simulator.
A fruit which boosts your attack damage, in Pet Simulator 99.
Boost makes it easy! You’ll be able to add, increase by percentage, multiply, and clamp a base value of your choice. Base values can be numbers, or NumberRanges. This can be used in multiple ways to account for a variety of use cases!
A Boost object has 5 important methods.
SetAdd: Add to the result value!
SetPercent: Add by a percentage of the result value!
SetScale: Multiply the result value!
SetClamp: Set the maximum result value!
SetActive: Set whether any modifier is active!
We will get to all of them later! API will also be provided later.
Why use a module?
So you just wanted to use basic math, huh? Let’s follow that through with Bee Swarm Simulator!
local beetleAmuletWalkspeed = 3
local antAmuletWalkspeed = 2
local hasteWalkspeed = 2
local hasteplusWalkspeed = 5
local moonAmuletWalkspeed = 0
local snailAmuletWalkspeed = 1
local beeWalkspeed = 2
-- etc...
humanoid.Walkspeed = 16 + beetleAmuletWalkspeed + antAmuletWalkspeed + hasteWalkspeed + hasteplusWalkspeed + moonAmuletWalkspeed + snailAmuletWalkspeed + beeWalkspeed -- etc...
Not super fun, or readable. With the boost module, you can dynamically set all these modifiers outside the script, which makes your code future proof!
This module was created specifically with Bee Swarm Simulator in mind, because they use a lot of boosters. My goal is to allow you to make something like that!
Use Cases
1: Potions, Amulets, and Charms
This use case will allow potions or some sort of ingame booster to change your values.
View Examples
local Boost = require(game.ReplicatedStorage:WaitForChild("Boost"))
local Walkspeed = Boost.new(16)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 16
Walkspeed:SetAdd("WalkspeedPotion", 4)
print(`My walkspeed now is {Walkspeed:Get()}!`) --> 20
In this code example, you can see a potion is increasing your walkspeed by 4 studs/second. How does it work? Let me explain each line.
1: Requiring the Boost module
2: Creating a new boost with the base value 16
4: Print the result value (16)
6: Set the WalkspeedPotion
flag to the rule Add 4
8: Print the result value (20)
These flags allow you to control the modifiers dynamically. We wouldn’t want to push a rule with no name because we can’t remove it that way!
The SetAdd
method adds to the result value. These are calculated first.
What if we wanted a bad potion? Like in minecraft, you could be hit with slowness or poison.
-- code from before
Walkspeed:SetPercent("SlownessPotion", -0.75)
print(`My walkspeed now is {Walkspeed:Get()}!`) --> 8
As you can see, your walkspeed went down by 75%. The SetPercent
counts 1 as 100%, -1 as -100%, and everything in between as well. These are calculated second.
Let’s make it more drastic. Let’s freeze the player next!
local Boost = require(game.ReplicatedStorage:WaitForChild("Boost"))
local Walkspeed = Boost.new(16)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 16
Walkspeed:SetScale("FreezeEffect", 0)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 0
Great! The SetScale
method allows you to multiply the result value. These are calculated third.
What if we went too far? Let’s also clamp the walkspeed so it must NOT go over a certain value.
local Boost = require(game.ReplicatedStorage:WaitForChild("Boost"))
local Walkspeed = Boost.new(16)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 16
Walkspeed:SetScale("Hyperspeed", 5)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 80
Walkspeed:SetAdd("WalkspeedPotion", 16)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 160
Walkspeed:SetClamp("TerminalVelocity", 92)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 92
The SetClamp
value sets the maximum value the result must be. These are calculated fourth.
2: Upgrades and Levels
We can also use this to better automate value modifiers based on your level!
View Examples
local Boost = require(game.ReplicatedStorage:WaitForChild("Boost"))
local Walkspeed = Boost.new(0)
Walkspeed:SetAdd("Level1", 16)
Walkspeed:SetAdd("Level2", 20)
Walkspeed:SetAdd("Level3", 24)
Walkspeed:SetAdd("Level4", 28)
Walkspeed:SetActive("Level1")
print(`My walkspeed is {Walkspeed:Get()}!`) --> 16
Walkspeed:SetActive("Level2")
print(`My walkspeed is {Walkspeed:Get()}!`) --> 20
Walkspeed:SetActive("Level3")
-- etc...
In the code example, you can set the walkspeed depending on the users level. This can also be done with SetScale
, but personally I will use this as it is more readable, and easier to softcode.
But wait, what is SetActive
? Well, this method forces only a specific modifier to be active. All the other ones will not be used! This is useful for setting modifiers based on your level.
Too restricting, though? Use the second parameter that sets whether it is active or not! This will only affect the specific modifier.
local Boost = require(game.ReplicatedStorage:WaitForChild("Boost"))
local Walkspeed = Boost.new(0)
Walkspeed:SetAdd("Level1", 16)
Walkspeed:SetAdd("Level2", 20)
Walkspeed:SetAdd("Level3", 24)
Walkspeed:SetAdd("Level4", 28)
local function setLevel(x: number)
for i = 1, 4 do
Walkspeed:SetActive("Level" .. i, i == x)
end
end
setLevel(1)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 16
setLevel(2)
print(`My walkspeed is {Walkspeed:Get()}!`) --> 20
setLevel(3)
-- etc...
Text Representation
Want to show the user what boosts they have? Use GetText()
! This returns a list of strings, showing what all the modifiers are doing and what they are.
local Boost = require(game.ReplicatedStorage:WaitForChild("Boost"))
local Walkspeed = Boost.new(16)
Walkspeed:SetAdd("Speed Potion", 4)
Walkspeed:SetAdd("Group Reward", 2)
print(Walkspeed:GetText())
--[[ Output:
[1] = "Speed Potion: +4",
[2] = "Group Reward: +2"
]]
You’ll have to continue these strings with the word ‘Walkspeed’ in your code.
Detecting Changes
In each boost, there is a Changed
event. You can connect to this when you add any modifiers, or set a new base value! This is likely where you will use the value, if not used in a while loop or stepped connection.
You can then use the Get()
method to get the result value.
Calculation Logic
The result value starts off with a base. This is assigned as the first parameter in Boost.new()
. You can also set a new base value later with Set(x)
.
Then, Adds
are calculated. They add a number to the result value.
You can set new adds using SetAdd(flag, x)
.
Next, Percentages
are calculated. They take the current result value, multiply it by the percentage, and then add it to itself. For example, +50% = x + x * 0.5
or x * 1.5
.
You can set new percentages using SetPercent(flag, x)
.
Now, Scales
are calculated. Simply, they multiply the result value.
You can set new scales using SetScale(flag, x)
.
Finally, Clamps
are calculated. This prevents the result value from going over x
.
You can set new clamps using SetClamp(flag, x)
After all this, you can get the result value using Get()
.
API
Hint: All number
types also include NumberRanges!
Super.new(x: number)
Returns a new Boost object.
x: Base value
Boost:Set(x: number)
Set the base value.
x: New base value
Boost:SetAdd(flag: string, x: number|boolean)
Add a value to the result.
flag: The modifier’s name
x: (number
) How much to add
x: (boolean
) Whether the modifier should be enabled or not
Boost:SetPercentage(flag: string, x: number|boolean)
Add to the result by a percentage.
flag: The modifier’s name
x: (number
) How much to add (100% is 1)
x: (boolean
) Whether the modifier should be enabled or not
Boost:SetScale(flag: string, x: number|boolean)
Multiply the result value.
flag: The modifier’s name
x: (number
) How much to multiply by
x: (boolean
) Whether the modifier should be enabled or not
Boost:SetClamp(flag: string, x: number|boolean)
Clamp the result value.
flag: The modifier’s name
x: (number
) The maximum result value
x: (boolean
) Whether the modifier should be enabled or not
Boost:SetActive(flag: string, x: boolean?)
Set whether this flag is active, or the only one active.
flag: The modifier’s name
x: (boolean
) Whether the modifier should be enabled or not
x: (nil
) Set this as the only active modifier
Boost:Get(): number
Get the result value.
Boost:GetBase(): number
Get the base value.
Boost:GetText(prefix: boolean): {string}
Returns a text representation of the modifiers that will be applied.
prefix: Show the{name}:
prefix in each text?
Boost:GetAdd(): number
Return the sum of all the add modifiers.
Boost:GetPercent(): number
Return the sum of all the percent modifiers.
Boost:GetScale(): number
Return the sum of all the scale modifiers, plus 1.
Boost:GetClamp(): number
Return the maximum value the clamp value can be.
tostring(Boost): string
Get the name of the boost. Format:
<Booster {base}>
Boost(): number
Get the result value.
Conclusion
If you find any bugs, I will try and fix them! Just be sure to provide a reproduction script that causes the bug, and some context as well.
Any typos on this topic or in the code? That’s bad! DM about those so I can fix them.
Known Bugs
- None