I have a sprinting mechanic in my game that makes an audio play a breathing sound when sprinting. The longer you sprint, the louder the audio gets. But the starting volume is so loud. So, in my script, I set the volume to 0.01, along with the sound’s settings in the workspace. Here’s the LocalScript that plays the audio:
local UIS = game:GetService('UserInputService')
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')
local player = game.Players.LocalPlayer
local NormalWalkSpeed = 10
local NewWalkSpeed = 20
local power = 10
local isPlaying = false
local sprinting = false
local breathe = workspace.breathing
breathe.Volume = 0.01
repeat wait() until game.Players.LocalPlayer.Character
local character = player.Character
UIS.InputBegan:connect(function(key, gameProcessed)
if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
character.Humanoid.WalkSpeed = NewWalkSpeed
sprinting = true
if not breathe.IsPlaying then
breathe:Play()
breathe.Looped = true
isPlaying = true
end
breathe.Volume = 0.01
while power > 0 and sprinting do
power = power - .1
breathe.Volume = breathe.Volume + 0.01
Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
wait()
if power <= 0 then
character.Humanoid.WalkSpeed = (NormalWalkSpeed)
end
end
end
end)
UIS.InputEnded:connect(function(key, gameProcessed)
if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
character.Humanoid.WalkSpeed = NormalWalkSpeed
sprinting = false
while power < 10 and not sprinting do
power = power + .03
breathe.Volume = breathe.Volume - 0.01
Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
wait()
if power <= 0 then
character.Humanoid.WalkSpeed = NormalWalkSpeed
end
end
end
end)