How could i Debounce The audio

I Have a ObjectValue inside my script that Stores the Sound object the i want to play
im trying to make a debounce so when the player clicks the first time it plays
and when the player clicks the second time it stops
now the problem is it dosent work

script.Parent.MouseButton1Click:Connect(function()
	local isplaying = false
	if isplaying == false then
		isplaying = true
		script.Parent.Play_Sound.Value:Play()
	else
		if isplaying == true then
			isplaying = false
			script.Parent.Play_Sound.Value:Stop()
		end
	end
	
	
end)



“now the problem is it dosent work” is a very vague statement. Remember, we can’t read your mind.

Does the sound not play ever?
Does the sound play and not shut off?
You change the value of isplaying every time the function is called, so it always does the if isplaying == false section of the script.
Do you get errors in the Output Window when testing?
Is the Sound parented inside the same Part the script is in?
How does the other script reference the Play_Sound.Value
Why not just Play the sound, instead of changing a value required by another script?

local isplaying = false
script.Parent.MouseButton1Click:Connect(function()
	if isplaying == false then
		isplaying = true
		script.Parent.**soundname**:Play()
	else
		if isplaying == true then
			isplaying = false
			script.Parent.**soundname**:Stop()
		end
	end
end)
2 Likes
script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Play_Sound.Value.IsPlaying then
		script.Parent.Play_Sound.Value:Stop()
	else
		script.Parent.Play_Sound.Value:Play()
	else
end)

Something like this:

Waiting = false
WaitTime = 2 -- Change this to however you like.

Button.Activated:Connect(function() -- This is so it can be Activated on all Devices (idk thats what people tell me)
if not Waiting then -- Not sure why this works but it does, im stupid
Waiting = true -- Delay Starts
Sound:Play() -- Da Sown Plaes
task.wait(WaitTime) -- Delay Time
Waiting = false -- Delay Ends
end
end)

If you want to add a wait until the sound is finished (Looking at it again, doesnt appear to be the case), follow the post above mine.

look when I Click The button the audio plays
but when I Click it again the sound restarts

and no i dont have an error in my output

image

is causing the issue, move it out of the event

1 Like

pro im feeling very very very Stubid at myself for not thinking about that
thank you soo much @SelDraken

1 Like

So basically what I showed you in the script I posted earlier?
Dude.

Actually our replies are similar but not quite the same, I leave out the isPlaying variable, and instead just do a check for Sound.IsPlaying

1 Like

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