How can I wait for the sound to end to play it again?

So, I’m testing out horror game mechanics. I have a script that makes you sprint, and when you do, it plays a breathing sound that gets louder as you continue sprinting. The thing is, if you tap space multiple times, the sound restarts, and it doesn’t sound very good. What I want is for the sound to continue playing until it stops, then it can play again.

The script (Not mine):

local UIS = game:GetService('UserInputService')



local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')



local player = game.Players.LocalPlayer



local NormalWalkSpeed = 16

local NewWalkSpeed = 30



local power = 10



local sprinting = false

local breathe = workspace.breathing



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
		
		breathe:Play()
		breathe.Volume = 0.1

		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

			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)

Hopefully this can get worked out.

try sound.ended event

extra characters to make 30

I’ve tried that, but I’m not sure why it doesn’t work.

This is what I’ve been trying:

UIS.InputBegan:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then

		character.Humanoid.WalkSpeed = NewWalkSpeed

		sprinting = true
		
		if breathe.Ended then breathe:Play()
		breathe.Volume = 0.1

		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)

breath.Ended:Connect(function())

or

breath.Ended:Wait()

try this “breath.Stopped:Wait()
wait(0.5)
breath:Play()”

The breathe.Ended:Connect(function()) seems to be working, but the thing is, how can I get it to start playing the first time, as the breath hasn’t even started, it can’t end.

Try to add a debounce that prevents the sound from playing again while the sound is playing. This can be done by declaring a boolean variable like “local isPlaying = false”. Whenever the button is pressed, you will check - via an if statement - if isPlaying is set to false and set it to true if that’s the case. Then you can wait for the sound to finish playing (this can be done with a simple wait() function) and after that, set isPlaying back to true.

if not breathe.IsPlaying then
   breathe:Play()
end
1 Like

Yeah that’s a simpler way to do it.

Thanks, that worked! Now all I have to do is some minor tweaks to make the breathing keep going if the stamina bar is low. I might come back here if I can’t figure out how to make that work. Thanks again!