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
001function BP()
002    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-.06, 0, 0)
003    script.Parent.ClickSound:Play()
004    script.Parent.ClickDetector.MaxActivationDistance = 0
005end
006function BU()
007    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(.06, 0, 0)
008    script.Parent.ClickDetector.MaxActivationDistance = 10
009end
010function onClicked()
011    local a = math.random(1, 24)
012    if a == 1 then
013        BP()
014        game.ReplicatedStorage.Coins:Clone().Parent = game.Workspace
015        script.Parent.Sounds.Coinsound:Play()
View all 190 lines...

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:

1function onClicked()
2    if Debounce == true then
3        Debounce = false
4--stuff that happens here--
5--Lets add a cooldown here, so debounce will not become true again until the wait is over--
6wait(30)
7Debounce = true
8    end
9end)

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

1Debounce = 0
2function BP()
3If Debounce == 0 then
4    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-.06, 0, 0)
5    script.Parent.ClickSound:Play()
6    script.Parent.ClickDetector.MaxActivationDistance = 0
7Debounce = 1
8end
9end

Then for your BU

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

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