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

How do I implement debounce into this script?

Asked by 6 years ago
function BP()
    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-.06, 0, 0)
    script.Parent.ClickSound:Play()
    script.Parent.ClickDetector.MaxActivationDistance = 0
end
function BU()
    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(.06, 0, 0)
    script.Parent.ClickDetector.MaxActivationDistance = 10
end
function onClicked()
    local a = math.random(1, 24)
    if a == 1 then
        BP()
        game.ReplicatedStorage.Coins:Clone().Parent = game.Workspace
        script.Parent.Sounds.Coinsound:Play()
        wait(3)
        BU()
    elseif a == 2 then
        BP()
        game.ReplicatedStorage.Doge:Clone().Parent = game.Workspace
        script.Parent.Sounds.Dogesong:Play()
        wait(12.669)
        BU()
    elseif a == 3 then
        BP()
        game.ReplicatedStorage.Jail:Clone().Parent = game.Workspace
        script.Parent.Sounds.Jailsound:Play()
        game.ReplicatedStorage.Jail2:Clone().Parent = game.Workspace
        wait(5)
        BU()
    elseif a == 4 then
        BP()
        script.Parent.Sounds.MoneyParticle:Play()
        game.ReplicatedStorage.MoneyParticle:Clone().Parent = game.Workspace
        wait(4.5)
        BU()
    elseif a == 5 then
        BP()
        script.Parent.Sounds.Dinner:Play()
        game.ReplicatedStorage.Dinner:Clone().Parent = game.Workspace
        wait(5)
        BU()
    elseif a == 6 then
        BP()
        game.ReplicatedStorage.Sign:Clone().Parent = game.Workspace
        script.Parent.Sounds.Sign:Play()
        wait(7)
        game.Workspace.Sign.Sound:Play()
        game.ReplicatedStorage.Explode:Clone().Parent = game.Workspace
        wait(2)
        BU()
    elseif a == 7 then
        BP()
        game.ReplicatedStorage.Laser:Clone().Parent = game.Workspace
        script.Parent.Sounds.Laser:Play()
        wait(3)
        BU()
    elseif a == 8 then
        BP()
        game.ReplicatedStorage.DarkMatter:Clone().Parent = game.Workspace
        wait(10)
        BU()
    elseif a == 9 then
        BP()
        game.ReplicatedStorage.Lava:Clone().Parent = game.Workspace
        wait(7)
        BU()
    elseif a == 10 then
        BP()
        game.ReplicatedStorage.Phonebooth:Clone().Parent = game.Workspace
        wait(10)
        BU()
    elseif a == 11 then
        BP()
        game.ReplicatedStorage.Rotating:Clone().Parent = game.Workspace
        wait(10)
        BU()
    elseif a == 12 then
        BP()
        game.ReplicatedStorage.PlatformStand:Clone().Parent = game.Workspace
        script.Parent.Sounds.Fall:Play()
        wait(5)
        BU()
    elseif a == 13 then
        BP()
        game.ReplicatedStorage.Fancy:Clone().Parent = game.Workspace
        script.Parent.Sounds.Fancy:Play()
        wait(10)
        script.Parent.Sounds.Fancy:Stop()
        BU()
    elseif a == 14 then
        BP()
        game.ReplicatedStorage.what:Clone().Parent = game.Workspace
        script.Parent.Sounds.what:Play()
        wait(2.808)
        BU()
    elseif a == 15 then
        BP()
        game.ReplicatedStorage.woah:Clone().Parent = game.Workspace
        game.ReplicatedStorage.woah2:Clone().Parent = game.Workspace
        script.Parent.Sounds.woah:Play()
        wait(10)
        script.Parent.Sounds.woah:Stop()
        BU()
    elseif a == 16 then
        BP()
        game.ReplicatedStorage.Toilet:Clone().Parent = game.Workspace
        script.Parent.Sounds.Plop:Play()
        wait(3)
        BU()
    elseif a == 17 then
        BP()
        script.Parent.Sounds.FUN:Play()
        wait(.5)
        script.Parent.Fire.Enabled = true
        wait(2)
        script.Parent.Fire.Enabled = false
        wait(1)
        game.ReplicatedStorage.Uranium:Clone().Parent = game.Workspace
        wait(2)
        game.ReplicatedStorage.Bombs:Clone().Parent = game.Workspace
        wait(2)
        game.ReplicatedStorage.Explode:Clone().Parent = game.Workspace
        BU()
    elseif a == 18 then
        BP()
        script.Parent.Sounds.Dance:Play()
        game.ReplicatedStorage.Dance:Clone().Parent = game.Workspace
        game.ReplicatedStorage.Dance2:Clone().Parent = game.Workspace
        wait(18)
        script.Parent.Sounds.Dance:Stop()
        BU()
    elseif a == 19 then
        BP()
        game.ReplicatedStorage.Record:Clone().Parent = game.Workspace
        wait(13)
        BU()
    elseif a == 20 then
        BP()
        game.ReplicatedStorage.Lava2:Clone().Parent = game.Workspace
        wait(5.9)
        BU()
    elseif a == 21 then
        BP()
        game.ReplicatedStorage.Piano:Clone().Parent = game.Workspace
        wait(10)
        BU()
    elseif a == 22 then
        BP()
        game.ReplicatedStorage.Diamonds:Clone().Parent = game.Workspace
        script.Parent.Sounds.Minecraft:Play()
        wait(7)
        game.ReplicatedStorage.Steve1:Clone().Parent = game.Workspace
        wait(5)
        game.ReplicatedStorage.Steve2:Clone().Parent = game.Workspace
        wait(8)
        game.ReplicatedStorage.Steve3:Clone().Parent = game.Workspace
        wait(2)
        game.ReplicatedStorage.Steve4:Clone().Parent = game.Workspace
        wait(5)
        script.Parent.Sounds.Minecraft:Stop()
        game.Workspace.Steve1:Destroy()
        game.Workspace.Diamonds:Destroy()
        game.Workspace.Steve2:Destroy()
        game.Workspace.Steve3:Destroy()
        game.Workspace.Steve4:Destroy()
        BU()
    elseif a == 23 then
        BP()
        game.ReplicatedStorage.Barry:Clone().Parent = game.Workspace
        script.Parent.Sounds.yalikejazz:Play()
        wait(3)
        script.Parent.Sounds.Jazz:Play()
        wait(10)
        game.Workspace.Barry:Destroy()
        script.Parent.Sounds.Jazz:Stop()
        BU()
    elseif a == 24 then
        BP()
        script.Parent.Sounds.Thomas:Play()
        wait(3)
        game.ReplicatedStorage.Thomas:Clone().Parent = game.Workspace
        wait(4.3)
        game.Workspace.Thomas:Destroy()
        script.Parent.Sounds.Thomas:Stop()
        BU()
    end
    end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

So basically if you look at the BP function then you can see that I make the MaxActivationDistance for the click detector 0. Then after a certain amount of time (depending on the outcome) the BU goes into play and reactivates the button essentially. This is my attempt to make it so the button isn't spam clicked. Howver this doesn't work all the time and I want to replace this system with debounce, however, I don't know how to and using debounce for every outcome would be very tedious.

3 answers

Log in to vote
1
Answered by 6 years ago

Making a debounce is actually quite simple! At the beginning of your script, simply make a variable that will represent your debounce. This can literally be named anything, as long as it dosnt end up interfering with another variable. I very much encourage you to give it its own unique name. Set it to true or false, it really dosnt matter either way, but we will have to remember what it is set to later on.

Lets say I named my debounce "Debounce" and I set it to true. (local Debounce = true)

Now for the next part, we will add an if statement after the function is triggered. It seems your looking to implement the debounce into the function on line 10. ("function onClicked()")

Immediately after line 10, we would add an if statement checking if the debounce is true or false

Example:

function onClicked()
    if Debounce == true then
        Debounce = false
--stuff that happens here--
--Lets add a cooldown here, so debounce will not become true again until the wait is over--
wait(30)
Debounce = true
    end
end)

This will make it so the function will not run again until debounce is true again, since debounce will not become true until the wait() ends, the button will not work again until the amount of time I put in the wait. Let me know if you have any more questions!

0
i know that, just in this situation idk how to do it Aelerity 5 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Don’t worry mate I’ve gotcha back... hopefully.

For the BP

Debounce = 0
function BP()
If Debounce == 0 then
    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-.06, 0, 0)
    script.Parent.ClickSound:Play()
    script.Parent.ClickDetector.MaxActivationDistance = 0
Debounce = 1
end
end

Then for your BU

function BU()
    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(.06, 0, 0)
    script.Parent.ClickDetector.MaxActivationDistance = 10
- Then put your wait time here with wait()
Debounce = 0
end

That should be everything, I’m really tired so expect something from me to be 50/50

0
I reccomend explaining what you did instead of just giving them the answer, if i didnt know what a debounce was, seeing this would give me a way to complete my script without learning. Therefore, leaving me with yet another problem in the future. EtherealTrin 45 — 6y
0
doesnt work Aelerity 5 — 6y
Log in to vote
0
Answered by 6 years ago

I still didn't get the help I needed, the reason I put the disabling clicking effect into these functions is so that it disables it for a certain amount of time for each outcome. Having one set wait time will not work. Remember, I want to replace just setting the MaxClickDistance to 0 with debounce

Answer this question