I’m trying to figure out how to make a value increase the closer a player gets to a part
Example:
Lets say we have a value and that value has a min and max and MIN = 5 and MAX = 10 how would I make it so that if your 50 studs away from the part the value equals the MIN but as you get from 50 to 0 studs away then the value gets closer to the MAX value?
I would appreciate it if anyone could help me out.
–first u need to find the distance.
Local part1 = game.workspace.part1
Local part2 = game.Players.LocalPlayer.Character.HumanoidRootPart
Local distance =
(part1.Position - part2.Position).Magnitude
– now let’s keep max distance as 10
– and min distance as 5
Local max = 10
Local min = 5
– now we want to calculate from 50.
Local requiredval = 10-(distance*(5/50))
–[[So now if we are 24 studs away answer would be 7.6 , if we are 50 studs away answer would be 5, if we are 0 studs away answer would be 10.]]–
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
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.
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.
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
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.