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

Why is my Debounce Failing...?

As the Title says, my Debounce in a script is not working. I am probably doing something extremely stupid which I am overlooking. Anyways, here is the script.

local DefenceBush = game.Workspace.DefenceBush



DefenceBush.TouchMe.Touched:connect(function(Hit)
	if Hit.Name ~= "Handle" and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health ~= 0 then
		local DB = false
		if DB == false then DB = true
			wait(60)
			DB = false
		end
	end
end)

All that occurs is the second a player makes contact with the Berry Bush, we check to see if It's a player that is alive and then I make my Debounce (DB) as false and check if it's false, if so, make it true then continue the script which decided randomly whether the player get 1-3 berries then a 60 second cooldown occurs and DB becomes true thus making the Berries visible. The player should not be able to receive berries when they touch the Bush during the cooldown, however they do.

2 answers

✓ Accepted answer

Your use of the debounce pattern is very subtly wrong.

Normally, you always want to use local variables, so that functions don't have side-effects--everything that a function/loop/condition computes doesn't affect anything else.

However, in this case, the point of DB is supposed to affect other functions -- when something else touches the button, it needs to know that DB is supposed to be true.

You want to define DB before the definition of the function.

local DB

DefenceBush.TouchMe.Touched:connect(function(Hit)
	if Hit.Name ~= "Handle" and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health ~= 0 then
		DB = false
		....

I really recommend a better name than "DB". Maybe alreadyTouching or disabled would be much clearer.

1 comment

0

Thank you for the assistance.

Declare DB outside the function. It recreates DB everytime someone touches it.

local DefenceBush = game.Workspace.DefenceBush

local DB = false -- Moved it to here, this should prevent it from being recreated

DefenceBush.TouchMe.Touched:connect(function(Hit)
    if Hit.Name ~= "Handle" and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health ~= 0 then
        if DB == false then DB = true
            local Plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
            local Get = DefenceBush.Berries:GetChildren()
            for _, Berry in pairs(Get) do
                Berry.Transparency = 1
            end
            local More = math.random(0,5)
            local DBerry = game.ReplicatedStorage.DefenceBerry:Clone()
            local DBerry2 = game.ReplicatedStorage.DefenceBerry:Clone()
            local DBerry3 = game.ReplicatedStorage.DefenceBerry:Clone()
            if More == 3 then
                DBerry.Parent = Plr.Backpack
                DBerry2.Parent = Plr.Backpack
            elseif More == 5 then
                DBerry.Parent = Plr.Backpack
                DBerry2.Parent = Plr.Backpack
                DBerry3.Parent = Plr.Backpack
            else 
                DBerry.Parent = Plr.Backpack
            end
            wait(60)
            DB = false
            for _, Berry in pairs(Get) do
                Berry.Transparency = 0
            end
        end
    end
end)

Let me know how that works and if you have any questions!

EDIT: BlueTaslem gives a much better explanation though!

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