Hi! I need help with the sound of the wagon. i am relatively new to scripting.
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.
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.
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.
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.
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.
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.
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.
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