Help to make magnitude based deceleration effect

Hi everyone,

Last night I had just finished my lock-on system for a smooth melee combat experience however now I face another issue. The lock-on will spaz out when the camera collides or gets too close with the enemy and this happens because it will do everything in its power to try to keep the mouse locked on.

I want to create some sort of deceleration effect to slow down the player as they get too close to the enemy in order to maintain proper distance between the enemy and the player. I have an idea for this but I have no way to approach it and since I’m relatively new to the roblox API, this is an issue I cannot tackle alone.

In this image above depicts the distance between the enemy and the player. The GRAY line represents the DEADZONE where the deceleration does not take affect, and the BLUE line represents the starting position for the deceleration. For example purposes, as you can see from the starting point, the player still maintains 16 walkspeed and slowly decends as the player gets closer. This is pretty much all I am trying to do. Any response is greatly appreciated, thank you very much for reading.

You would probably have to GetPlayers or try to find a humanoid near and find the distance between you in a loop until the other player is dead