How do I make a value increase the closer you get to a part

For min and max value you can probably use math.clamp

your method is correct but I think if the distance is more than 50 then, the value becomes lower than the min value

for example,
distance = 55
requiredVal = 10 - (distance*(5/50))
requiredVal = 10 - (55*(5/50))
= 10 - 55*0.1
= 10 - 5.5
= 4.5
And 4.5 is lower than the min value 5

So you could just add an if statement like

if distance >= 50 then
    requiredVal = 5
else
    requiredVal = 10 - (distance*5/50)
end
1 Like

I’m trying to set the min to 0.5 and the max to 0.05 but it just gives me a negative number.

Why don’t you try using math.clamp?

Yeah I tried using it but I don’t quite know how math.clamp works even after reading the description.

You should try using magnitude! More about that here.

Is there anyway I can make it so the min is lower then the max?

First of all, to get the percentage of the distance in the interval (0, 50), which should just be distance/50 (where distance is (character.HumanoidRootPart.Position - part.Position).Magnitude).

To apply this percentage to the MIN and MAX, you first need to apply the percentage on the distance between MAX and MIN (meaning MAX - MIN) so you get (MAX - MIN)*(distance/50), and add MIN to that since it’s our starting value.

At the end, you have this:

MIN + (MAX - MIN)*(distance/50)

What if the distance was greater than 50? (MAX - MIN) would overshoot too. You simply clamp the end value, with the minimum value being MIN, and maximum being MAX.

math.clamp(MIN + (MAX - MIN)*(distance/50), MIN, MAX)
9 Likes

If the MAX is less than the MIN, it’s gonna give you a negative value, which is totally logical. Why are you making the MAX smaller than the MIN anyways? Just flip them so MAX is 0.5, and MIN is 0.05, or just take the math.abs of the original result.

The reason is because I’m trying to make a heartbeat sound the closer you get to the part and I want the sound to get faster the closer you get and I have a cooldown before the heartbeat sound plays again. I want the value to get lower the closer you get to the part so the heartbeat will play faster.

1 Like

In the formula I provided above, the more the distance between the character and the part converges to 0, the more your cooldown value converges to MIN, meaning it should be what you want.

math.clamp( value, min, max ) just returns the input value but within the range. Examples:
math.clamp(-6, 1, 5) would return 1
math.clamp(12, 1, 5) would return 5
math.clamp(3, 1, 5) would return 3

The 1 - shouldn’t be there actually, the end product is

math.clamp(MIN + (MAX - MIN)*(distance/50), MIN, MAX)

Ohh okay but now it says MAX must be greater then MIN in the output is there anyway to get around this?

As I said earlier, MAX needs to be greater than MIN. Just exchange their values, MAX is 0.5 and MIN is 0.05.

Yeah, I’m a bit dumb but its working perfectly fine now. I’ll mark your original formula as the solution now.

1 Like

Can you maybe figure out why that does this?
I’m making a script that does what you think it’s for, but for some reason the parts get smaller the closer
it is.


may you release the full script? i dont understand what to do TvT

1 Like

I ran into the same problem and realized that the equation they provided actually decreases the value instead of increasing. The correct equation would be:

math.clamp(MIN + (MAX - MIN)*(1-(distance/maxDistance)), MIN, MAX)
7 Likes

is there a way to do this but with vector3s because when i multiply vector threes with the result it doesnt multiply it how i want it to