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