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

How to add a debounce to prevent people gaining many wins in a second?

Asked by 3 years ago

I need help on how to add a debounce because if I don't add that when people step on the part they will get many of the stat "Wins" in a second. Someone please help me!

"Wins" giver part script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        game.Players:FindFirstChild(hit.Parent.Name).leaderstats.Wins.Value += 1
    end
end)

2 answers

Log in to vote
0
Answered by 3 years ago
local debounce = false --Bool Value

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    if not debounce then
        debounce = true

            game.Players:FindFirstChild(hit.Parent.Name).leaderstats.Wins.Value += 1
        wait() --Waits before turning debounce back to false
        debounce = false --Turns debounce back to false
    end
    end
end)

If this didn't help, please don't downvote me. I'm really new to scripting and I'm just tryna' help. TY

0
That is how you add debounce, basically. Dehydrocapsaicin 483 — 3y
0
You can remove a couple of lines if you do if debounce then return end. It's basically the same thing. Dovydas1118 1495 — 3y
0
TYSM IT WORKED! I'm a worse scripter so to me learning how to add a debounce is like being a huge step up. Nitrolux200 62 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The previous answer is still faulty; use 2 scripts, 1 is called "WinnerCount"; the other called "Resetter" make Resetter disabled & leave WinnerCount enabled; both in the part that is acting as your finishline.

WinnerCount:

script.Parent.Resetter.Disabled = true
local debounce = false

script.Parent.Touched:Connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
        debounce = true
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
        script.Parent.Resetter.Disabled = false
        wait(3)
    end
end)

The Resetter script:

if script.Parent.CHANGEME.Disabled == false then
    script.Parent.CHANGEME.Disabled = true
        wait (30) --How long until the Finishline will work again, can be replace by a finishline position changer code line that undoes itself after a condition is met.
        script.Parent.CHANGEME.Disabled = false
end
0
what for. the other script works just fine though. Nitrolux200 62 — 3y
0
Maybe, but if the player accidentally steps on the win block twice, it'll probably double count it/ the finishline will only work once; if they do the same race/whatever, it wont work. FLIPPER4440 21 — 3y

Answer this question