Audio Playback Bug

I’m having trouble changing the PlaybackSpeed of this sound in the Roblox Client.

SmoothedSpeedFactor is calculated and then used to increase or decrease the playback speed of this sound playing on loop.

This works just as intented in studio, but once I open the game from Roblox and playtest, the sound stays at its original PlaybackSpeed and Volume. What’s the issue?

local yawDelta    = math.abs(currentYaw - previousYaw) / dt
local rawFactor   = math.clamp(yawDelta / CameraStat.RotationSpeed, 0, 1)
smoothedSpeedFactor = math.clamp(smoothedSpeedFactor + (rawFactor - smoothedSpeedFactor) * (1 - (0.003 ^ dt)),0,0.15) 

Sfx_Drone.PlaybackSpeed = 1 + smoothedSpeedFactor * 10
Sfx_Drone.Volume        = 0.01 + smoothedSpeedFactor * 0.8

I’ve found that

print("smoothed speed: ".. smoothedSpeedFactor)

actually prints a value in studio, but just “nan” when in the client?

smoothedSpeedFactor is becoming NaN most likely because one of these divisions is invalid

math.abs(currentYaw - previousYaw) / dt

or

yawDelta / CameraStat.RotationSpeed

For example, dt or CameraStat.RotationSpeed may be 0 or one of the yaw values may already be NaN math.clamp doesnt sanitize NaN so it then propagates into smoothedSpeedFactor, PlaybackSpeed and Volume

Try guarding the inputs

local function isFinite(value)
	return value == value
		and value ~= math.huge
		and value ~= -math.huge
end

if dt <= 0 or CameraStat.RotationSpeed <= 0 then
	return
end

if not isFinite(currentYaw) or not isFinite(previousYaw) then
	previousYaw = currentYaw
	return
end

local yawDelta = math.abs(currentYaw - previousYaw) / dt

if not isFinite(yawDelta) then
	return
end

local rawFactor = math.clamp(
	yawDelta / CameraStat.RotationSpeed,
	0,
	1
)

smoothedSpeedFactor += (rawFactor - smoothedSpeedFactor) * (1 - 0.003 ^ dt)
smoothedSpeedFactor = math.clamp(smoothedSpeedFactor, 0, 0.15)

Sfx_Drone.PlaybackSpeed = 1 + smoothedSpeedFactor * 10
Sfx_Drone.Volume = 0.01 + smoothedSpeedFactor * 0.8

You should also print the values before the calculation

print(currentYaw, previousYaw, dt, CameraStat.RotationSpeed)

One of those values will probably be invalid only in the live client. Also make sure previousYaw is initialized to currentYaw before the update loop starts

2 Likes