Speed Bar

I currently have this code:

draw.RoundedBox(0, 320, ScrH()- 70, math.Clamp(velocity, 0, 250), 40, Color(0,130,130))

250 being the width of the background

I would like to make it so that as your speed increases the bar increases with it but as your speed gets higher the rate of increase of the bar is to get slower making it impossible to exceed 250px in width.

Snip, misread

I’ll give you a hint,




depending on how fast you want it to drop off

There’s no lua functions for that, you must apply maths, at the moment you are using linear expressions, you need to use exponential functions

The way I usually do it is this

desired_width = ( current_value /maxiumum_value ) *max_width

That’s turning the current speed into a percentage (without the *100) of the maximum speed and then multiplying the maximum width by the result to give you the length in pixels you want.
So if you are using speed and you wanted your bar to be 250 pixels maximum
[lua]local width = ( get_current_speed /LocalPlayer():GetMaxSpeed() ) *250[/lua]
I can’t remember how you get the player’s speed right now, but you should be able to find it by searching it up.

But he’s looking for something that increases the bar value according a curve

Having a look here, specifically at Method 3.

The HUD I’m making is to help me learn.
Would it be possible for some people to provide examples.

local velocity = math.Round(ply:GetVelocity():Length2D())
	draw.RoundedBox(4, 320, ScrH()- 70, 250, 40, Color(130,130,130))
//Moving Bar
	draw.RoundedBox(0, 320, ScrH()- 70, math.Clamp(velocity, 0, 250), 40, Color(110,110,110))
//Speed Indicator
	draw.SimpleText(velocity, "SpeedFont", 442, ScrH() - 50, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) 

Did you even look at the page I linked? It’s full of examples on how to interpolate to values…

We gave you enough data so you can work on your side
If you can’t explore and get your own ideas…Learning will be useless

Also, this is not something about a clear example, you’re asking about maths, braden gave you an excellent resource about fading and exponential functions, you really should know about maths if you want to apply them

This is not a lua lesson, it’s something more like a math lesson
As you know, a linear expression looks like:

y = x + a

Where y it’s the output, x our var (In our case velocity) and a it’s a constant, we need to fill 500px, so:

500 = (max_velocity) + 0 //Because we don’t need to fill pixels with extra data

But with this, we don’t will get the curve, because a curve it’s made by an exponencial function like

y = x² + a
y = x * x + a

500 = (max_vel) * (max_vel) + a

So you will need to clamp the values or divide it, because exponential values are really strongers and can change their values on violent ways

2² = 4
3² = 9
4² = 16
5² = 25


max_width = (x * x)/a

Where “a” will be the value that lowers the final output

If you want more resource about exponential expressions, you can check the wiki, if you don’t know them, i recommend to you to learn them because will be really useful for your maths.

I know exponential functions but having a good example of how I would apply this in my code would be great help to me this is how I learn best. Tutorials help, but reading the code as a recipe really does help in my case.

So if you know them do it by yourself, i won’t code for you, i’ll guide you, we’ll guide you, we won’t code for you

And here are the examples

Don’t be lazy and start coding without need other people

I’m not asking for 50 lines of code here, I’m just saying a little example would help me GREATLY.