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 not working? [Solved]

local debounce = true

script.Parent.Touched:connect(function(part)
	local h = part.Parent:FindFirstChild("Humanoid")
	 if h ~= nil and debounce == true then
		wait()
		game.ServerStorage.t1:Clone().Parent = game.Workspace
		script.Parent.p:Play()
		local tp1 = game.Workspace.t1
		wait()
		script.Parent.BrickColor = BrickColor.new("Black")
		debounce = false
		--[[The following lines are optional in case you want a timer for a button to reactivate.
			
		wait(5) --Cooldown time. Can be changed to anything depending on what you want.
		script.Parent.BrickColor = BrickColor.new("Really blue") --Change the color to whatever you
																 --want.
																
		debounce == true --This re-enables the script. Don't mess with this unless you want to break
						 --the script.
		
		--]]
	end
end)

Sorry for the slightly messy code. Basically, I want this button to disable itself after you touch it WITHOUT disabling the script, because I want a "cooldown" time for scenarios later in my game. I would very much appreciate assistance on this, as I am not too sure how debouncing works. Thank you!

1 answer

✓ Accepted answer

Here you go!

The explanation is in the script. :] Also, you should make the debounce a little longer because it's not really a debounce if you're only using it for not even a second.

local debounce = true 

script.Parent.Touched:connect(function(part)
    local h = part.Parent:FindFirstChild("Humanoid")
     if h ~= nil and debounce == true then
        wait()
	debounce = false --ADD THIS HERE BECAUSE THEN WHEN YOU TOUCH THE PART, YOU WON'T BE ABLE TO ACTIVATE IT UNTIL YOU CHANGE THE DEBOUNCE TO TRUE AGAIN.
        game.ServerStorage.t1:Clone().Parent = game.Workspace
        script.Parent.p:Play()
        local tp1 = game.Workspace.t1
        wait() 
        script.Parent.BrickColor = BrickColor.new("Black")
        debounce = true -- CHANGES TO TRUE SO YOU CAN REACTIVATE IT.

        --[[The following lines are optional in case you want a timer for a button to reactivate.

        wait(5) --Cooldown time. Can be changed to anything depending on what you want.
        script.Parent.BrickColor = BrickColor.new("Really blue") --Change the color to whatever you
                                                                 --want.

        debounce == true --This re-enables the script. Don't mess with this unless you want to break
                         --the script.

        --]]
    end
end)

1 comment

0

A tip, for a short cut. Instead of debounce == true, just do debounce by itself since debounce means true so it will run.

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