So I’m trying to do it so when the monster is nearby, a heartbeat sound from Workspace will start beating. The closer the monster is, the faster the heartbeat plays. Haven’t found much useful threads for this except one but when I tried implementing it to my game, for some reason the sound wasn’t playing at all for me even though there was no errors in the Output. The script seemed to work for the guy that got the question answered.
The setup:
The heartbeat sound in Workspace
The properties
The monster in Workspace
The LocalScript I was talking about in StarterCharacterScripts
The code inside
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local run = game:GetService("RunService")
local part = workspace.Monster.HumanoidRootPart
local sound = workspace.NearbyHeartbeatSound
run.RenderStepped:Connect(function()
local maxdist = sound.RollOffMaxDistance
local dist = math.clamp((hrp.Position-part.Position).Magnitude,0,maxdist)
local far = (dist/maxdist) -- low pitch when close, high pitch when far
local close = 1-far -- high pitch when close, low pitch when far
sound.PlaybackSpeed = close
end)
Like I said earlier, not so sure why things aren’t working even with no errors in the Output.
Footage
I was hoping for the sound PlaybackSpeed to start at 0.9 with the range starting at 100 studs away and the PlaybackSpeed topping at 1.2 once he’s very close. I hope it makes sense as I’m a bit confusing with explaining at times haha, I’d appreciate some help!
local players = game:GetService("Players")
local player = players.LocalPlayer
local run = game:GetService("RunService")
local part = workspace.Monster.HumanoidRootPart
local sound = workspace.NearbyHeartbeatSound
run.RenderStepped:Connect(function()
local distance = player:DistanceFromCharacter(part.Position)
--Use this value to determine the sound's speed.
end)
So obviously this is in a localscript, the distance will determine the playback speed. So let’s say the distance is 50, you can have an equation like: sound.PlaybackSpeed = (100 / distance) So it would be very slow, and you can change the 100 to something that you want. The playbackspeed in that example would be 2. So the higher you make the first number (100) the slower it gets if you are far or close. and lower it makes it faster. Hope this helps.
Ohh alright. If the RollOffMaxDistance property of the audio is set to be 100, would this make sense
local players = game:GetService("Players")
local player = players.LocalPlayer
local run = game:GetService("RunService")
local part = workspace.Monster.HumanoidRootPart
local sound = workspace.NearbyHeartbeatSound
run.RenderStepped:Connect(function()
local distance = player:DistanceFromCharacter(part.Position)
sound.PlaybackSpeed = (100 / distance)
end)
Would this start playing the audio from 100 studs away? Tried doing it like this into the LocalScript but the sound was still not playing at all. Also, would the RollOffMinDistance interfer with things? I have it set to 0 so I’m not sure if that’s causing any problems. Basically trying to make it starts playing but slow from 100 studs away at 0.9 speed and when it’s very close it tops at 1.2 speed max. Not sure if my explanation makes sense but yeah, the sound isn’t playing still. Is it because the way I wrote the code right now?
I guess it would but it would be very loud if you are 1 stud close (100 times normal sound), basically if you want to figure out what number to put. Just pretend distance = 1 and see how much you want the sound to be if the distance is 100. If the music is not playing, did you play the sound and did you loop it?
local players = game:GetService("Players")
local player = players.LocalPlayer
local run = game:GetService("RunService")
local part = workspace.Monster.HumanoidRootPart
local sound = workspace.NearbyHeartbeatSound
run.RenderStepped:Connect(function()
local distance = player:DistanceFromCharacter(part.Position)
sound:Play()
sound.PlaybackSpeed = (70 / distance)
end)
The audio is kinda stuttering now in a way and cutting off a bit not playing all the way through.
What the normal sound sounds like
Sound playing once in game and the AI is near
Sounds like a motorcycle Would I have to use a debounce or something like that? It’s playing louder too. I’d want the volume to remain at 1 but only the speed increases yk.
That’s because you are playing it every time. So every frame you get it restarts the sound. Make the music start playing outside of the render stepped function.
local players = game:GetService("Players")
local player = players.LocalPlayer
local run = game:GetService("RunService")
local part = workspace.Monster.HumanoidRootPart
local sound = workspace.NearbyHeartbeatSound
sound:Play()
run.RenderStepped:Connect(function()
local distance = player:DistanceFromCharacter(part.Position)
sound.PlaybackSpeed = (70 / distance)
end)
Probably just find the magnitude of the player’s HRP position and the monster’s position by adding them and getting .Magnitude. Then, divide the magnitude by like a lot because it might be a big number. Then just take that and make the PlaybackSpeed equal to that.
Edit: Oh this probably works better instead of magnitude.
Looks like sound:Play() runs once (put it outside the function as recommended) but that’s from you spawning in and not the code below it like it should be doing.
Not sure if you heard it but it played when I spawned in and doesn’t go again with the AI being near.
Alright excellent, got it to play again thanks. So close to finally pulling this off but the last 2 things that I’ve been trying to do is keeping the volume at 2 regardless of the distance and not increase from there. Minimum audio speed would be 0.9 but the fastest it can get is 1.2. Atm it plays super fast and is a bit quiet/loud depending on the distance.
Quick footage:
What adjustments should I do to the code provided to fix it? That’s pretty much it afterwards.
Actually it doesn’t matter any more about the volume as it’s not too bad tbh. Most of all just would like for the pitch to not go above 1.2 regardless even if the monster is right next to you.
What would this line do? Would it help with it? Tried it and didn’t really change much.
This is how things look like still.
local players = game:GetService("Players")
local player = players.LocalPlayer
local run = game:GetService("RunService")
local part = workspace.Monster.HumanoidRootPart
local sound = workspace.NearbyHeartbeatSound
sound:Play()
run.RenderStepped:Connect(function()
local distance = player:DistanceFromCharacter(part.Position)
sound.PlaybackSpeed = (70 / distance)
end)
I know that
sound.PlaybackSpeed = (70 / distance)
has to do with the PlaybackSpeed changing depending on the distance from you and the part but how could I make the pitch not go above 1.2 from here?