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

How do I add debounce to a script?

When I asked my first question, it was answered but somebody else also suggested adding a debounce script to my restricted door. Could I get help with that? http://wiki.roblox.com/index.php?title=Debounce

local door = script.Parent
 
function open()
	-- This function will open the door.
	door.CanCollide = false -- Make players able to walk through the door.
 
	for transparency = 0, 1, .1 do
		door.Transparency = transparency
		wait(.1)
	end
end
 
function close()
	-- This function will close the door.
	for transparency = 1, 0, -.1 do
		door.Transparency = transparency
		wait(.1)
	end
 
	door.CanCollide = true -- Make players unable to walk through the door.
end
 
-- This function returns the player a certain part belongs to.
function get_player(part)
	--iterate over each player
	for _, player in ipairs(game.Players:GetPlayers()) do
		--if the part is within the player's character 
		if part:IsDescendantOf(player.Character) then
			--return the player
			return player
		end
	end
end
 
 
door.Touched:connect(function(part)
	-- If it isn't a character that touched the door, then we ignore it.
	local player = get_player(part)
	if not player then return end
 
	local allow = (
		player.Name == "TheRabster1428" or
		player.Name == "Whirlpooled" or 
		player.Name == "TylerTiger88" or 
		player.Name == "TylerTiger88V2" or
		player.Name == "lXMysteryXl" or
		player.Name == "AncientDinoDan" or
		player.Name == "djnightkiller" or
		player.Name == "danknoscoper12" or
		player.Name == "JuntheDoge" or
		player.Name == "peopledumb" or
		player.Name == "iliketranezz" or
		player.Name == "Quoonisbest" or
		player.Name == "willywaffle99"
	)
	if allow then
		open()
		delay(4, close)
	end
end)

1 comment

0

make sure search your question on the roblox developer wiki first before posting here, as it usually has the answer ;)

4 answers

Just add in a variable named something like local debounce = false and then set it to true when they touch it. Have it wait a second and then set it to false. Before you go and set it to true, though, have it check to see if it's false.

1 comment

0

I'm new to this so I don't know where or how to add anything like that.

To add a debounce, you can use a bool value to indicate whether the debounce is on or off. When it is on, simply don't open the door even if an allowed player touches it.

For example:

local debounce = false

part.Touched:connect(function(touch)
	if not debounce then
		debounce = true

		touch:Destroy()
	end
	
	wait(1)
	debounce = false
end)

The above example makes sure the script can only be run once a second at maximum.

Adding a debounce i simple, Example;

local debounce = true

script.Parent.Touched:Connect(function() -- change this if you want
if debounce == true then -- checks if the debounce is true
debounce = false --changes it to false
-- put your script in here 
debounce = true --sets it to true after the script has run
end

You can find more here: https://wiki.roblox.com/?title=Debounce

A debounce can actually be anything. It does not have to be a boolean. It can be a number or a string too.

HOW TO USE DEBOUNCE

You use debounce by first defining the variable outside of the given function:

DebounceNumber = 1 -- Notice what I'm doing here

Then start your function, and when you start your function, add this:

function ILoveRoblox()
	if DebounceNumber == 1 then
		DebounceNumber = 2 -- Again, doesn't have to be a number, it can be a boolean or string.
	

Followed by the rest of the script:

		print("I love Roblox")
		wait(3)
		DebounceNumber = 1
	end
	if not DebounceNumber == 1 then break -- Debounce line
end
script.Parent.Touched:Connect(ILoveRoblox)

As I said before, a debounce can be anything (boolean, string, number). A debounce takes action when the value of the debounce variable is changed, and an if statement declares that "Hey, if this variable is equal to this value, start the function. If it is not equal to this value, stop the function immediately." That is the true way to use a debounce variable.

3 comments

0

Why are you returning nothing at the end of a function?

0

What are you talking about? It returns the end value.

0

hold on a sec

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