Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I add Debounce to my button?

I have a script that plays music once a button is pressed, but I want to wait until the song that is playing is over and only then be able to click again and have another song play. How can I wait for the end of a song? Any and all help is appreciated.

Storage = game:GetService("ReplicatedStorage").SoundStorage

SoundTable = {Storage.OneDance, Storage.Freaks, Storage.Spaz, Storage.DontWorryChild, Storage.MonsterMash, Storage.Centuries}
-- Run numbers a different way.

function ChooseMusic(Table)
	for i,v in pairs(Table) do
		local x = math.random(1,#Table)
		print(x)
		if x == 1 then
			local Sound =  SoundTable[1]
			local SoundClone = Sound:Clone()
			SoundClone.Parent = workspace.Speaker
			SoundClone:Play()
			break
		elseif x == 2 then
			local Sound = SoundTable[2]
			local SoundClone = Sound:Clone()
			SoundClone.Parent = workspace.Speaker
			SoundClone:Play()
			break
		elseif x == 3 then
			local Sound = SoundTable[3]
			local SoundClone = Sound:Clone()
			SoundClone.Parent = workspace.Speaker
			SoundClone:Play()
			break
		elseif x == 4 then
			local Sound = SoundTable[4]
			local SoundClone = Sound:Clone()
			SoundClone.Parent = workspace.Speaker
			SoundClone:Play()
			break
		elseif x == 5 then
			local Sound = SoundTable[5]
			local SoundClone = Sound:Clone()
			SoundClone.Parent = workspace.Speaker
			SoundClone:Play()
			break
		elseif x == 6 then
			local Sound = SoundTable[6]
			local SoundClone = Sound:Clone()
			SoundClone.Parent = workspace.Speaker
			SoundClone:Play()
			break
		end
	end
end

script.Parent.ClickDetector.mouseClick:connect(function()
	ChooseMusic(SoundTable)
end)

1 answer

✓ Accepted answer

In the sound properties, there is a property of TimeLength. So you can grab that TimeLenth and wait(TimeLength). That's the only way I know.

This discussion is preserved as it was. New answers, comments, and votes are closed.