Exponential Curve for player speed

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.

Exponential equation, I’d use x^2. It increases fast, though.

1 Like

You could do something like

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.

1 Like

That’s not an exponential equation!

x^x has a steeper curve
Just apply a multiplier where necessary

Fiddle around with desmos to figure out what expression would be ideal for you

1 Like

I’d probably put Speed = Max / Penalty

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!)

1 Like

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

The last line should read

local penalty = math.clamp(clampedPenalty, 0, math.huge)

I still don’t understand how to apply this technique. I’m not sure what a lot of the arguments mean or where my actual equation goes.

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.

			local humanoid = char.Humanoid or char:WaitForChild("Humanoid")
			
			local threshold = 70
			local clamp = math.clamp((char:GetAttribute("Weight") - threshold), 0, math.huge)^1.05
			local penalty = math.clamp(clamp, 0, math.huge)
			
			humanoid.WalkSpeed = humanoid.WalkSpeed - penalty

Even with an incredibly low number, the speed still drops to almost a halt when I add only 10 units of weight past the threshold

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.

That did it, thank you so much. I still absolutely hate math

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?

As the penalty is inside a math.clamp, it will never be below 0.

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.

That’s it, thank you for all your help, sorry if it took me a bit to understand

Ah, my mistake, it’s a parabola, my response was a bit rushed. An exponential equation would be 2^x.

Thanks for the correction.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.