How would I make a heartbeat sound

Hello there people.

I’ve been working on a horror game and I need to implement a heartbeat system. Now, normally you would get a looped heartbeat sound and change the PlaybackSpeed based on the distance of the monster. However, I do not want to change the Speed or Pitch of the sound. I’d rather have a “Rate” that the sound is being played at based on the distance of the monster.

So far this is my script:

local function CloneSound(Sound)
	local ClonedSound = Sound:Clone()
	ClonedSound.Parent = Sound.Parent
	ClonedSound:Play()
	game:GetService("Debris"):AddItem(ClonedSound, ClonedSound.TimeLength / ClonedSound.PlaybackSpeed)
end

while true do
	if Entities:FindFirstChild("Him") then
		local Entity = Entities.Him
		if Entity:FindFirstChild("Torso") then
			local Distance = (Entity.PrimaryPart.Position - HumanoidRootPart.Position).Magnitude
			task.spawn(function()
				CloneSound(Entity.Torso.Que)
			end)
			task.wait(math.max(0.5, Distance/50))
		else
			task.wait(0.1)
		end
	else
		task.wait(0.1)
	end
end

Now that the script works fine if you move away from the monster but it’s very delayed if you move towards the monster. That is the problem since I want the rate of the heartbeat sound to keep updating even if you go closer to the monster. I’m sure the problem is caused by this line:

task.wait(math.max(0.5, Distance/50))

Because it will wait the time until it’s done. While it’s waiting it cannot update the distance between the player and the monster and it can’t play the sound either.

I’ve tried to make a for loop that breaks if the distance between the player and the monster changes by 10 studs but that just makes the sound play repeatedly at a very high rate.

If anyone has suggestions on how to fix this, I’d appreciate it.

1 Like

If I understand this correctly all you need to do is make a sound thats in the monster and then adjust the RollOffMin/MaxDistance in its properties

Thank you for your reply!

Sadly this is not the problem. I’m not trying to increase the range of the sound. I’m trying to increase the rate at which the sound is played without changing the PlaybackSpeed. For example, if the monster is 100 studs away it will play the sound every 2 seconds and if the monster is 50 studs away it will play the sound every 1 second.

Sorry for necro posting, but try the PitchShiftSoundEffect object.