You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Heartbeat sound should Change speed depending on magnitude
What is the issue? Sound doesn’t increase playback speed when magnitude decreases
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 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
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.