How do you use debounce?

I don’t know if this is the right way to use debounce, so I just wanted to ask y’all if 1. I’m using debounce correctly, 2. If not, what did I do wrong, and how can I fix it?

local access = false
script.Parent.Touched:Connect(function(hit)
       if hit.Name == "Keycard 3" or "Keycard 4" then
       game.ReplicatedStorage.TweenDoors:Fire()
       access = true
       wait(5)
       access = false
    else
       print("Not keycard")
       game.ReplicatedStorage.Error:Fire()
    end
end)

Serverscript in a Part

You need to make sure that the denounce isn’t active after checking if the player has a keycard. If it’s false, do the script, if it’s true, do nothing.

This is an example:

local Debounce = false

Button.MouseClick:Connect(function()
if Debounce == false then
Debounce = true
      print("Button was pressed")
      wait(2)
     Debounce = false
   end
end)

I hope this helped you.

4 Likes

There is an entire article on debounce if you are interested: https://developer.roblox.com/en-us/articles/Debounce

and @CAP7A1N @sjr04 something like this?

    ''''
     local Debounce = false

    script.Parent.Touched:Connect(function(hit)
         if Debounce == false then
            if hit.Name == "Keycard 3" or "Keycard 4" then
               game.ReplicatedStorage.TweenDoors:Fire()
                Debounce = true  
                 wait(2)
                  Debounce = false
            end
       end
   end)

         '''
2 Likes

You see, you need to check if the debounce is false or not, and if it is, it will be true. Then it will be false again after the wait.

Yes exactly like that, good job!
Did it work?

Made some amendments and pointers on your code, just for you to be aware of:
Debounce is there to stop things happening multiple times in a single instance - for example a door. You may want to make it so the server notifies the client when the door can be handled again because wait(2) isn’t reliable.

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if not debounce then
		if hit.Name == "Keycard 3" or hit.Name == "Keycard 4" then -- amended else the statement will always be true as the second one would be a presence check.
			debounce = true -- do the debounce first before the operation, this is to stop :Fire() from being done twice.
			game.ReplicatedStorage.TweenDoors:Fire()
			wait(2) -- you may want to handle this through the server
			debounce = false
		end
	end
end)

Now it won’t tween the doors (30 chars)

Why don’t you do the tweening inside the same server script?

If they keycard is a tool, then technically you wouldn’t be “hitting” the door with the keycard. You’d be hitting it with a part which is a child of the tool. You’ll need to program it to handle this logic.

Just because the keycard is called “Keycard 2” doesn’t make every child of it the same. Iterate through the players, checking their characters for tools, if the tool is named “Keycard 2” or “Keycard 3” then you should do a :IsDescendantOf() check.

1 Like

I made this script that detects if a tool is touching a part, it should work for you:

local KeyCardHole = game.Workspace.Part
local Debounce = false

KeyCardHole.Touched:Connect(function(other)
if Debounce == false then
   Debounce = true
if other.Parent:FindFirstChild("Humanoid") then
   local player = game.Players:GetPlayerFromCharacter(other.Parent)
        if other.Parent:FindFirstChild("KeyCard") and other.Parent.KeyCard:IsA("Tool") then
			--Run code here
           end
	     end
        end
      wait(3)
      Debounce = false
end)

	

It should work, but idk

I adjusted your code a little but it still doesn’t work

   ''''
local debounce = false
local KeyCardHole= game.Workspace.Part

script.Parent.Touched:Connect(function(other)
    if other.Parent:FindFirstChild("Humanoid")  and debounce == false then
       local player = game.Players:GetPlayerFromCharacter(other.Parent)
         if other.Parent:FindFirstChild("KeyCard 4") and other.Parent.Can:IsA("Tool") then
            debounce = true
            game.ReplicatedStorage.TweenDoors:Fire()
            wait(2)
             debounce = false
         end
        end
      end)

       '''

Change the can to KeyCard 4 I used it for a purchasing system and one of the item was a can of beans

Because other, again, will now be the parent of the part that hit - if its a tool then the Parent is likely to be the tool itself.

I made this for you which should work:

function GetKeycardByHit(hit)
	for _, Player in ipairs(game.Players:GetPlayers()) do
		if hit:IsDescendantOf(Player.Character) then
			for _, obj in ipairs(Player.Character:GetChildren()) do
				if obj:IsA("Tool") then
					if string.len(obj.Name) >= 7 then
						local tabl = string.split(obj.Name, " ")
						if tabl[1] == "Keycard" then
							return obj
						end
					end
				end
			end
		end
	end
	return false
end
local debounce = false
local KeyCardHole= game.Workspace.Part

script.Parent.Touched:Connect(function(other)
    if other.Parent:FindFirstChild("Humanoid")  then
if debounce == false then
       local player = game.Players:GetPlayerFromCharacter(other.Parent)
         if other.Parent:FindFirstChild("KeyCard 4") and other.Parent["KeyCard 4"]:IsA("Tool") then
            debounce = true
            game.ReplicatedStorage.TweenDoors:Fire()
            wait(2)
             debounce = false
         end
        end
      end)

Try that maybe.

It won’t work, as I said to him, the parent isn’t going to be the player, its going to be the tool - it wont work for all cases either especially with complex tools.

What would you put here? (30 chars)

Nothing, thats to split the string. Seeming as a common term in all your keycards is “Keycard” and then a blank space " " I decided we’ll look for that as the first term.

string.split splits strings based on the string placed in the second parameter.

No errors anywhere but it isn’t working