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

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

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

✓ Accepted answer
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

3 comments

0

That is how you add debounce, basically.

0

You can remove a couple of lines if you do if debounce then return end. It's basically the same thing.

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.

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

2 comments

0

what for. the other script works just fine though.

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.

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