Sigh... more debounce help please

I have a team door in my game that works in the script below.

local team = game.Teams.(team name)
local door = script.Parent
local sound = script.Parent.Sound

door.Touched:connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.Team == team then
			door.CanCollide = false
			door.Transparency = 1
			sound:Play()
			wait(3)
			door.CanCollide = true
			door.Transparency = .5
		end
	end
end)

I need to debounce the script and I added the below script. The only problem is it only works every now and then. Sometimes it just acts like a solid wall.

local team = game.Teams.(team name)
local door = script.Parent
local sound = script.Parent.Sound
local debounce = false

door.Touched:connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
	if not debounce then
	debounce = true
	
		if humanoid then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr.Team == team then
				door.CanCollide = false
				door.Transparency = 1
				sound:Play()
				wait(1)
				door.CanCollide = true
				door.Transparency = .5
				wait(1)
				debounce = false	
			end	
		end
	end
end)

Anyone think they know why this script only works some of the time. Sometimes it works as it should and the debounce even works correctly. I must have something wrong to make it not work sometimes.

I bet it has something to do with the last wait(1).

Thanks.

1 Like

You should check your debounce after it is verified that the object touching is actually a player instance and part of the team :slight_smile:

local team = game.Teams.(team name)
local door = script.Parent
local sound = script.Parent.Sound
local debounce = false

door.Touched:connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")	
	if humanoid then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.Team == team then
			if not debounce then
				debounce = true
				door.CanCollide = false
				door.Transparency = 1
				sound:Play()
				wait(1)
				door.CanCollide = true
				door.Transparency = .5
				wait(1)
				debounce = false	
			end
		end	
	end
end)

First off, it might be an NPC that touchs. Which will return nil and broke the script. You need to check if plr is exist or no first

Addin onto what @bookgamery555gta said, this is the final script:

local team = game.Teams.(team name)
local door = script.Parent
local sound = script.Parent.Sound
local debounce = false

door.Touched:connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")	
	if humanoid and debounce == false then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			if plr.Team == team then
				debounce = true
				door.CanCollide = false
				door.Transparency = 1
				sound:Play()
				wait(1)
				door.CanCollide = true
				door.Transparency = .5
				wait(1)
				debounce = false	
			end
		end	
	end
end)

If you want to know how to use debounces, this is how to do it:

local debounce = false

part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and debounce == false then
		debounce = true
		local player = game.Players:FindFirstChild(hit.Parent.Name)

		-- INSERT CODE HERE

		-- OPTIONAL: You can insert a wait(1) here if you want the touched event to have a cooldown of 1 second

		debounce = false
	end
end)

Optional: Try to avoid having so many tab spaces and keep code clean to make your dev life easier in the future.

local Players = game:GetService("Players") -- Important Roblox services that you will reuse. Notice capital letter
local Teams = game:GetService("Teams")

local door = script.Parent -- Object references
local sound = door.Sound

local team = Teams["TEAM NAME HERE"] -- Variables
local debounce = false

local function openDoor(hit)
	if debounce then return end -- Debounces are usually placed at the beginning and the end. This is because of how important they are and accidentally missplacing a "Debounce = false" can cause havoc
	debounce = true
	
	local character = hit.Parent -- Clean variable names in a neat order
	local humanoid = character:FindFirstChild("Humanoid")
	local player = Players:GetPlayerFromCharacter(character)
	
	if character and humanoid and player and player.Team == team then -- All conditional statements in one place
		door.CanCollide = false
		door.Transparency = 1
		sound:Play()
		task.wait(1) -- Space after wait to show a cut or delay in the code
		
		door.CanCollide = true
		door.Transparency = .5
		task.wait(1)	
	end
	
	debounce = false -- IMPORTANT: Allow debounce to fire again. Notice it's at the end
end

door.Touched:Connect(openDoor) -- Notice how the function and connection are kept separate. This allows you to call the same function again if you need to later on