Disable Sound File after it has Ended

so, I want to disable a sound file after it has ended, the way I’m doing it is transferring the sound file to another folder once it ends. But then it just gives errors when the script cant find the sound file, so I want to know if there’s a better way to do this other than using debounce as it will also affect all the other sound files that will be playing after.

this is the main LocalScript

local Player = game:GetService("Players")
local SoundService = game:GetService("SoundService")
local Subtitle = Player.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("Subtitles"):WaitForChild("Subtitle")
local SoundFolder = game.Workspace.Narration
local DisabledAudio = game.Workspace.Narration.DisabledAudio
local Remotes = game.ReplicatedStorage.Remotes

--//Intro Narration
Remotes.Intro.VoiceIntro1.OnClientEvent:Connect(function(plr)
	local VoiceIntro1 = SoundFolder.Intro.voice_intro_1_00
	VoiceIntro1:Play()
	Subtitle.Text = "Text"
	if VoiceIntro1.Ended:Wait() then
		VoiceIntro1.Parent = DisabledAudio
	end
end)

“Disable”? Do you mean stop playing?

If you want to make it so they won’t be able to hear it again just set the volume to 0.

I have also thought about that, but it wouldn’t work so well since it will affect the subtitles also.

What are you trying to achieve exactly? “Disable” is vague.

I’m simply trying to prevent the Sound from being played more than once, so basically, I want it to play and after it ends, it will be unplayable

.Ended is an Event, I don’t know why you’re trying to sanity check it through an if statement

Since you’re calling it with a Wait() function, all you just need to do is remove the conditional check and replace it with this as the function will yield until the specified action has been completed:

VoiceIntro.Ended:Wait()

No need to check for something like that

If you want to keep using your method of reparenting you could do a check to see if it still exists by adding an if. This should fix your “But then it just gives errors when the script cant find the sound file” error.

Remotes.Intro.VoiceIntro1.OnClientEvent:Connect(function(plr)
	local VoiceIntro1 = SoundFolder.Intro:FindFirstChild("voice_intro_1_00")
	if VoiceIntro1 then
		VoiceIntro1:Play()
		Subtitle.Text = "Text"
		VoiceIntro1.Ended:Wait()
		VoiceIntro1.Parent = DisabledAudio
	end
end)
1 Like

I was actually gonna try this, and thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.