Dynamic sound for the wagon

Hi! I need help with the sound of the wagon. i am relatively new to scripting.

  1. What do you want to achieve?
    I would like to make the sound play when the train carriage moves and the sound stops when the wagon is stationary.
  2. What is the issue?
    I know how to speed up the sound as the wagon speed increases, but I don’t know how to make the sound play and stop depending on whether the wagon is moving. It seems like I would answer myself, but when the car is stationary then the speed of sound does not go to zero but stays at the lowest speed.
  3. What solutions have you tried so far?
    I’ve searched this forum, but I’ve only found ways to increase the speed of sound - I already know that.
    also i have tried different codes that don’t work.

I will give a colloquial example here to make it easier to understand:
if speed > 1 then
sound.playing = true
else
sound.playing = false
i know it’s something related to magnitudes, but if i try to find speed by magnitude then it doesn’t work.

Thanks!

2 Likes

Hello, how are you doing?

If you already have a sound variable with the ID of the sound you would like it to play and the “Looped” is set to true, you can do a while loop and check if the position of the wagon has changed.

If it did, it plays the sound, if it didn’t it will stop/don’t play the sound

local Wagon = --directory of the wagon goes here, if it's on the workspace you can do game.Workspace.Wagon

local Sound = Wagon.Sound --The sound instance with the ID and Looped set to true.

local Position = Wagon.PrimaryPart.Position --Change the "PrimaryPart" with the name of the part inside the Wagon that you set to be the Primary Part.

Wagon.PrimaryPart.Changed:Connect(function()
	if Position ~= Wagon.PrimaryPart.Position then
		Sound:Play()
		Position = Wagon.PrimaryPart.Position 
	end
end)

while wait(1) do
	if Position == Wagon.PrimaryPart.Position then
		Sound:Stop()
	end
end

If this doesn’t works, please reply to my message and tell me which errors there were, i’ll try to fix the code.
If you want more information about this, you can search up on the wiki about Position, Sound Parts, CFrame (if you want a more detailed stuff about position and rotation) and Body Velocity, if you want to do it about speed and not about position.

Hope i could help you, have a good day!

Note: The code only will work if the wagon is a model.

No errors are displayed, but the music does not start playing. I will also send a video. maybe i’m just stupid lol.

1 Like

You changed the variable Position to Wagon.Base.Position, but on the Changed and at the while loop it’s still Wagon.PrimaryPart.Changed

If this doesn’t work, i’ll review my code and try to find what is wrong

I fixed my bug,but sound not start playing while wagon moving.

So it won’t play when the wagon starts moving, but a little bit after it does?

no, i can speed up indefinitely, but sound not start playing.

Alright, i’ll review my code.
Hopefully i can find what is wrong.

local Wagon = --directory of the wagon goes here, if it's on the workspace you can do game.Workspace.Wagon

local Sound = Wagon.Sound --The sound instance with the ID and Looped set to true.

local Position = Wagon.PrimaryPart.Position --Change the "PrimaryPart" with the name of the part inside the Wagon that you set to be the Primary Part.


while wait(1) do
	if Position == Wagon.PrimaryPart.Position then
		Sound:Stop()
	end
	if Position ~= Wagon.PrimaryPart.Position then
		Sound:Play()
		Position = Wagon.PrimaryPart.Position 
	end
end

Try this out, if it doesn’t work add some prints inside the if statements, the while loop and prints of the position variable with the position of the primary part.

if statement print does not give me any output. They don’t perceive it as the part moves.

Both of the ifs give no print at all?

No, neither gives output. …

Are you sure the wagon is at the workspace or anything? It should’ve printed atleast one thing because the while wait(1) loop should’ve run.

Yes, the wagon is on the workspace and there have been no outputs since the game started. I tried again but nope.

That’s really weird…
I’m not that good in scripting, but according to what i did it should’ve printed atleast once…

Sorry if i couldn’t help you the way you wanted, but if you want more information you can click here, here and here if you still want to fix the problem

Again, i’m sorry i couldn’t fix your problem and i think there’s nothing more i can do. Have a good day.

However, thank you for your support! :slight_smile:

2 Likes

No problem, hope you can found the solution to your problem!

1 Like

The reason the code suggested by @TrashMakeRice isn’t working for you is that you already have a while-true-do loop at the start of your code. Also, the .Changed event doesn’t fire on physics-related changes (like position, cframe, velocity).
I attempted to edit the script to make it work in this scenario, but I haven’t tested it myself. I will post it here in a second.

1 Like

Now that you mentioned it, i noticed the while true do part, i literally have no idea why i didn’t notice that part.

I had no idea .Changed didn’t work on those things, i thought it was when anything of all the part’s property changed, thanks for letting me know!

This is the script I came up with:

local Audio = script.Parent

local speed = 0

local DriveSound = Audio:WaitForChild("DriveSound")

local Sound = Audio:WaitForChild("Sound")

Sound.Looped = true

DriveSound.Looped = true

while true do
	wait()

	speed = Audio.Velocity.Magnitude
	
	if speed >= 1 and (DriveSound.Playing == false or Sound.Playing == false) then
		
		DriveSound:Play()
		Sound:Play()
		
	else
		
		if speed < 1 then
			
			DriveSound:Stop()
			Sound:Stop()
			
		end
		
	end
	
	DriveSound.PlaybackSpeed = speed/100 + 0.1
	Sound.PlaybackSpeed = speed/100 + 0.1
	Sound.Volume = speed/100 + 0.1

end
1 Like