Sound doesn't increase playback speed when magnitude decreases

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Heartbeat sound should Change speed depending on magnitude

  2. What is the issue? Sound doesn’t increase playback speed when magnitude decreases

  3. What solutions have you tried so far? Yes, but none of them worked

local heartbeat = function(ai:Model):()
	local mod = get_conf(ai, false)
	local hb = script.hb
	if mod.EXTRA.HEARTBEAT then
		local beat_dist = mod.EXTRA.HEARTBEAT_DIST
		coroutine.wrap(function()
			while task.wait() do
				if cc.CFrame.Position.Magnitude < beat_dist then
					if not hb.IsPlaying then
						hb:Play()
					end
					hb.PlaybackSpeed = math.clamp(beat_dist/(ai.PrimaryPart.Position - cc.CFrame.Position).Magnitude, .8, 1.2)
				end
			end
		end)()
	end
end

This doesnt look right, am i reading this correctly on my phone?

it’s a coroutine.wrap method. It requires you to have the () to call the function.

1 Like

I missed that part in my hurried reading… thanks for clarifying.

no probs my guy.
I still need help though lol.

if cc.CFrame.Position.Magnitude < beat_dist then

This isn’t checking distance, but you have a valid distance check later. Here is modified code that should work.

while task.wait() do
	local distance = (ai.PrimaryPart.Position - cc.CFrame.Position).Magnitude
	if distance < beat_dist then
		if not hb.IsPlaying then
			hb:Play()
		end
		-- adding 0.1 so if distance is 0 you do not get a error
		hb.PlaybackSpeed = math.clamp(beat_dist/distance + 0.1, .8, 1.2)
	end
end

It’s not working, maybe it’s something with the collection service or the way I wrote my code.

thanks for responding though :slight_smile:

local heartbeat = function(ai:Model):()
	local mod = get_conf(ai, false)
	local hb = script.hb
	local beat_dist = mod.EXTRA.HEARTBEAT_DIST
	local con:RBXScriptConnection = game["Run Service"].Heartbeat:Connect(function()
		local distance = (ai.PrimaryPart.Position - cc.CFrame.Position).Magnitude
		if distance < beat_dist then
			hb.Volume = math.clamp(beat_dist/distance + 0.1, 0, 1.2)
			hb.PlaybackSpeed = math.clamp(beat_dist/distance + 0.1, .8, 1.2)
		else
			hb.Volume = 0
		end
	end)
end

Can you post the whole script? Your heartbeat’s type signature implies it’s supposed to return a function but it doesn’t. It’s name implies it’s supposed to return the heartbeat connection (or so I’d assume). There might be something wrong with how the function is used.