How to set the max possible sound pitch?

So the script down below makes the engine sound pitch be based on the car velocity but the problem is that the pitch doesn’t have limit. So for example if the car goes 200 km/h engine sound will get messed up.

How to set the max possible sound pitch?

local maxSpeed = 30

game:GetService("RunService").Heartbeat:Connect(function()

local velocity = script.Parent.Parent.Velocity.Magnitude 

script.Parent.PlaybackSpeed = (velocity / maxSpeed) + 0.6 end)

You can set limits on values through math.clamp(x,min,max). A post with a detailed explanation can be found here

1 Like

Ok I understood it, but how do I implement it in my script?

Something like this would work

local maxSpeed = 30

game:GetService("RunService").Heartbeat:Connect(function()
	local velocity = script.Parent.Parent.Velocity.Magnitude 
	local calculation = (velocity / maxSpeed) + 0.6 
	local min = 1
	local max = 3

	script.Parent.PlaybackSpeed = math.clamp(calculation,min,max)
end)

Just change the 1 in min and 3 in max to whatever values you want

1 Like