Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
Despayr 505 Moderation Voter
4 years ago
Edited 4 years ago

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)
Ad

Answer this question