How do I make a value smaller the closer a player gets to a part?

Hi there, I am trying to make a game similar to this Doom Mod, Creepy Doom WADs - do_not_play.wad - YouTube where you’re in a maze running away from the monster. The monster makes a noise which repeats faster and faster the closer you are to it, which I am trying to replicate.

The issue is, is that I’ve tried using lerp to change the threshold based of magnitude between two values, but that does not work as the change is rather in reverse (the closer you are, the slower the sound repeats).

I’ve attached my attempt below:

local function lerp(a,b,t)
	return a+(b-a)*t
end

local sound = script.Parent["boom sound"]
local pitch = sound.pitch
local nextbot = script.Parent.Parent
local threshold = sound.TimeLength

game.Players.PlayerAdded:Connect(function(plr)
	local connection : thread
	
	plr.CharacterAdded:Connect(function(char)
		sound.Volume = 10
		
		if connection then
			task.cancel(connection)
		end
		
		connection = task.spawn(function()
			while true do
				sound:Play()
				print(threshold)
				task.wait(threshold)
			end
		end)
		
		game:GetService('RunService').Stepped:Connect(function()
			local hrp : Part = char.HumanoidRootPart
			local distance = (hrp.Position - nextbot.HumanoidRootPart.Position).Magnitude
			
			threshold = lerp(sound.TimeLength,0.5,distance/10000)
			print(distance/10000)
		end)
	end)
end)
1 Like

If the effect is currently in reverse, then all you have to do is, to define the MaxEffect and substract the distance from that.
Edit: Or divide, it all depends on what effect you’re trying to achieve.

1 Like

local distance = (object1.Position - object2.Position).Magnitude right?
So using this calculation, use a while loop or changed event when the player moves, to update distance. Distance will be the value.

Except that would be too fast (after its divided)

Maybe try offsetting that value? (Sorry for the english)