Hi, I’m making a weight system and have completed the weight part of it, but still have to do the speed penalty part.
I’d like to make it so past a certain number (Player’s base weight), a penalty kicks in on the players speed when weight is added past that number. However, I’d like to make it exponential, so starting out you can go past the weight limit a good bit without much of a penalty, but as you go on, the penalty gets more severe for each unit of weight.
I know it involves some sort of graphing equation, I’m just not sure where to start. Any help would be appreciated, thanks.
local weightLimit = 10
local clampedPenalty = math.clamp((weight - weightLimit), 0, math.huge)^2
local penalty = math.clamp(penalty, 0, math.huge)
This would make the penalty be 0 until it crosses the weight limit, at which point it would start exponentially increasing.
If you want it to increase at a slower rate, you could change the ^2 to a smaller number. Just make sure it is above 1 as if it is below it will start decreasing instead.
This will make it so your speed will slow exponentially, but never reach zero. However, make sure to keep it 1 or above or you’ll end up going faster (or even infinitely fast if you have a penalty of zero!)
How would something like this be applied to player walkspeed, because I noticed in the penalty variable you use penalty in the clamp arguments, which is an unknown. In example, I’m using 2^x
weightLimit is the number at which you want the speed penalty to start applying at. This should be the maximum weight the player can carry unburdened. clampedPenalty is just for the exponential calculation. Penalty is the important number. This is a number that starts at 0 and rises as the player’s weight increases. You could set the player’s walkspeed to 16 - penalty to slow them down. (assuming the default walkspeed is 16). Again, you can change the rate at which the penalty increases by changing the 2 at the end of local clampedPenalty = math.clamp((weight - weightLimit), 0, math.huge)^2 to a lower number.
I should’ve been more clear. The number you are changing is how high the rate of change is for the weight. If it is 2 then it will go very quickly, but if it is 1 then it will still rise at a rate equal to the change. You could set the number back to 2 and instead divide penalty by a number that works better, for example humanoid.WalkSpeed = humanoid.WalkSpeed - (penalty / 250) I believe would result in the player no longer being able to move at about 60 above the limit.
Also I have this linked up to an AttributeChangedSignal() event, what method could I use to adjust this so that it doesn’t add a negative penalty no matter what, even if weight is removed?
Yeah but my problem is that because the penalty function is linked to the attribute changing, even if the weight decreases your speed gets slowed even further. So how could I determine whether to add or negate the penalty?
If you change humanoid.WalkSpeed = humanoid.WalkSpeed - penalty to humanoid.WalkSpeed = 16 - penalty then it will work. 16 is the default walkspeed so if you’ve changed that you’ll need to change this too.