How to set sound pitch based on car speed

I want to alter the pitch of a driving sound. Atm, it’s just a static loop (<1 second, looped) but I want to someone increase/decrease either the volume, pitch, idk, to make it sound like a car would when accelerating/driving.

I don’t know, I kinda just wrote this, assuming Velocity.Magnitude could be used to get like how fast the car is going, then doing something to it?

while true do
	-- Update drive sound
	local VelocityMagnitude = Vehicle.Body.VehicleSeat.Velocity.Magnitude
	Vehicle.Drive.Volume = VelocityMagnitude / (VelocityMagnitude * 2)
	
	wait()
end

This is both relating to how to script the difference, as well as what sound/effect/pitch things can I add to the sound to give it the sound I want

2 Likes

I made my chassis before, and I used ths code for car pitch (basic version):

local maxSpeed = 30

game:GetService("RunService").Heartbeat:Connect(function()
      local velocity = MyCar.Body.Seat.AssemblyLinearVelocity.Magnitude
      MyCar.EngineSound.PlaybackSpeed = (velocity / maxSpeed) + 0.6
end)

[Code Edit:] BasePart.Velocity is deprecated, so I replaced with BasePart.AssemblyLinearVelocity.

(BasePart.AssemblyLinearVelocity does the same job as BasePart.Velocity, I think they changed for better naming.)

Nevermind, I found this post about diffences BasePart.Velocity between BasePart.AssemblyLinearVelocity:

https://devforum.roblox.com/t/thoughts-on-deprecating-velocity/1024605/9

12 Likes

I just wrote it like this:

game:GetService("RunService").Stepped:Connect(function()
	local pitch = math.abs(script.Parent.Parent.Parent.Base.CFrame:vectorToObjectSpace(script.Parent.Parent.Parent.Base.Velocity).Z)/20
	script.Parent.PlaybackSpeed = 0.75 * math.max(pitch, 0.5)
	local findseat, nonfound = pcall(function()
		script.Parent.Parent.Parent.VehicleSeat["SeatWeld"]:GetFullName()
	end)
	if findseat then
		script.Parent.Playing = true
	elseif nonfound then
		script.Parent.Playing = false
	end
end)