Different sounds as vehicle accelerates/decelerates?

What are you trying to achieve?
I want to make a way for my trains to play different sounds as it speeds up or slows down and possibly add a better ‘fade’ effect. Currently, I have a script that uses Velocity.Magnitude from a part on the train and it plays only one sound on a loop until it reaches 40, where it stops intentionally.

What is the issue?
My script isn’t working as properly as it should. One thing I mean is that the volume increases too slowly when the train starts to move and doesn’t get loud enough until it’s at speed, which is unfortunately, after 40. People have also reported instances of faintly hearing the accelerating sounds when the train is completely stopped.

Another thing is like I said earlier, I want to add different sounds that play at different speeds (for example, if the train is going from 0 to 10, play a low-pitched motor sound (completely different sound or the same sound just at a different pitch) and then when it goes from 10 to 20, it plays a medium-pitched sound and so on. It would reverse this order when slowing down). However, I’m not that knowledgeable in scripting and I don’t know how to do this properly.

What solutions have you tried so far?
I have tried hiring other scripters (the ones I’m paired up with for my game’s development don’t specialize in sound scripts) but I got none. I looked at the Wiki for answers, but like I said, I can’t script very well and barely understood what it told me, even if it was what I was looking for.

Further details?
An example of the sound script I want is at 0:41 of this video: https://www.youtube.com/watch?v=7zhylhtkKXc. If you listen, you can hear the different pitched motor whines as it speeds up.

4 Likes

I’ve tried doing the whole “multiple sounds” thing before, but with cars. Trust me, doing one sound is so much nicer on the ears. Everytime the sound switches it’s audibly noticeable and very abrupt.

As far as making sure you can’t hear any sound when the train is stopped, you can run check statements like
if (velocity.magnitude < 2) and vehicleseat.throttle < .75 then
sound.isplaying = false

What this does is it checks to see if someone is applying throttle to the train (aka, is the train being driven or not) and check for speed (if it’s moving) and if it decides that the train is both not being driven and is not moving it will fail to play sound.

As far as getting a low pitch sound at the bottom end of speed that starts getting a higher pitch as the train speeds up, that’s simply math. And if you’d like to have, for example, a large increase of pitch at certain speeds, you just run more checks using if statements.

So, your overall script would be something like:
if (velocity.magnitude < 2) and vehicleseat.throttle < .75 then
sound.playing = false
elseif velocity.magnitude > 15 then
audio.playbackspeed = velocity.magnitude / 15
audio.volume = velocity.magnitude / 15
elseif velocity.magnitude > 5 then
audio.playbackspeed = velocity.magnitude / 10
audio.volume = velocity.magnitude / 10
sound.playing = true
end

Always check for a higher speed first in an if statement, because if you check for a low speed first, it’ll never make it to a higher speed. So for example,if the vehicle is traveling at 7 & if you have:
if speed >= 15 then – it’ll skip this because the speed is less than 15
if speed >= 5 then – it’ll hit this because the speed is greater than 5

vs if the vehicle is traveling at 17 & if you have:
if speed >= 5 then – it’ll hit this because the speed is greater than 5
if speed >= 15 then – it’ll won’t this because it already hit the statement before

Edit: In the video you have, the different pitch you hear isn’t due to speed. It’s because as the train is leaving the station, more throttle is applied. Unfortunately Roblox doesn’t really have throttles that can be applied heavier or ligther, but this can easily be done by also including a “transmission” like script into your train to disallow it to have full power until it hits a certain speed, and with that full power, you could introduce a different pitch by also including torque, so for example

audio.playbackspeed = (velocity.magnitude+seat.torque) / 15

3 Likes

Okay, thanks. But what if I want to add another sound pitch change at the next speed it reaches?

1 Like

just keep adding more elseif statements

So like
if (velocity.magnitude < 2) and vehicleseat.throttle < .75 then
sound.playing = false
elseif velocity.magnitude > 45 then
audio.playbackspeed = velocity.magnitude / 45
audio.volume = velocity.magnitude / 45
elseif velocity.magnitude > 35 then
audio.playbackspeed = velocity.magnitude / 35
audio.volume = velocity.magnitude / 35
elseif velocity.magnitude > 25 then
audio.playbackspeed = velocity.magnitude / 25
audio.volume = velocity.magnitude / 25
elseif velocity.magnitude > 15 then
audio.playbackspeed = velocity.magnitude / 15
audio.volume = velocity.magnitude / 15
elseif velocity.magnitude > 5 then
audio.playbackspeed = velocity.magnitude / 10
audio.volume = velocity.magnitude / 10
sound.playing = true
end

That being said though, think about the values mathematically when adjust them.
And feel free to adjust them around a lot. Remember though that the bottom one will always extend to the top one. So the Velocity.magnitude > 5 is going to be changing pitch and volume from 5-15 sps. (Meaning your pitch and volume range will be 5/10 (.5) to 15/10 (1.5))
And the next step up in this example would be from 15 to 25 with a pitch and volume range of 15/15 (1) to 25/15 (1.66666)

So play with the math to accomplish what you want.

7 Likes

If what I’ve said answers your questions sufficiently, click the little box with a check on my reply to mark it as the answer to your thread. If not, feel free to wait around till someone else provides a better answer and mark their’s as the solution. Marking solutions helps people in the forums decide which questions have been answered or unanswered yet.

2 Likes

What I have done is created a system in which the sounds cross-fade between each other at certain thresholds. It’s a bit complicated to set up, but I think a module could easily be built to handle it. At the end of the day, it’s like a “system of equations,” where you have an input (the speed), which then determines the volume for all the sounds.

I could maybe consider putting this together later today, since it would be useful for myself as well.

5 Likes

Do you have the link to it, idea sounds amazing!