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.