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

What do I add so effects and sounds are only played once if button is clicked multiple times?

Code I have right now:

	local debounce = true
		
	if debounce then      --Where on the script would I place the debounce stuff?
		debounce = false
		wait(1)
		debounce = true
	end
script.Parent.ClickDetector.MouseClick:Connect(function()                                                             
             --Do I put this in between local debounce and if debounce then?

	game.Workspace.ZooPhotographer.Camera.L.BrickColor = BrickColor.White()
	game.Workspace.ZooPhotographer.Camera.L.PointLight.Brightness = 1
	game.Workspace.ZooPhotographer.Camera.L.PointLight.Range = 8
	game.Workspace.ZooPhotographer.Camera.L.Material = Enum.Material.Neon
	script.Parent.CameraFlash:Play()
	wait(0.25)
	game.Workspace.ZooPhotographer.Camera.L.BrickColor = BrickColor.Black()
	game.Workspace.ZooPhotographer.Camera.L.PointLight.Brightness = 0
	game.Workspace.ZooPhotographer.Camera.L.PointLight.Range = 0
	game.Workspace.ZooPhotographer.Camera.L.Material = Enum.Material.Glass
	script.Parent.CameraFlash:Stop()

end)
	

What should I fix?

I feel like math would be involved but I am not so sure.

Something along the lines of "'If the button is clicked more than once, the effects and sound cannot be played, but the wait time in the debounce section must pass until the button can be clicked again'".

Thanks! :)

1 answer

✓ Accepted answer

You would place the debounce within the function itself so that when it fires, it sets it to false.

local debounce = true

script.Parent.ClickDetector.MouseClick:Connect(function()

	if not debounce == true then return end

	debounce = false

	workspace.ZooPhotographer.Camera.L.BrickColor = BrickColor.White()
	workspace.ZooPhotographer.Camera.L.PointLight.Brightness = 1
	workspace.ZooPhotographer.Camera.L.PointLight.Range = 8
	workspace.ZooPhotographer.Camera.L.Material = Enum.Material.Neon
	script.Parent.CameraFlash:Play()
	wait(0.25)
	workspace.ZooPhotographer.Camera.L.BrickColor = BrickColor.Black()
	workspace.ZooPhotographer.Camera.L.PointLight.Brightness = 0
	workspace.ZooPhotographer.Camera.L.PointLight.Range = 0
	workspace.ZooPhotographer.Camera.L.Material = Enum.Material.Glass
	script.Parent.CameraFlash:Stop()

	wait(3)
	debounce = true --only needed if you wish for the debounce to refresh after 3 seconds

end)

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