Does anybody know why my debounce only works the first time?

I was writing code for a keycard reader. There’s supposed to be a debounce, but it only works the first time. Any time the employee’s keycard touches the hitpart after that, there’s no debounce.
Here’s the code so far:

local ts = game:GetService("TweenService")
local keycardtweeninfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)
local tweenmodule = require(game:GetService("ReplicatedStorage"):FindFirstChild("TweenModel"))
local db = false

--setup
--hides animated keycards
for i,v in pairs(script.Parent.KeycardReader.Employee:GetDescendants()) do
	if v:IsA("TextLabel") then else
		if v:IsA("SurfaceGui") then v.Enabled = false else
			pcall(function()
				v.Transparency = 1
			end)
		end
	end
end
for i,v in pairs(script.Parent.KeycardReader.Security:GetDescendants()) do
	if v:IsA("TextLabel") then else
		if v:IsA("SurfaceGui") then v.Enabled = false else
			pcall(function()
				v.Transparency = 1
			end)
		end
	end
end
for i,v in pairs(script.Parent.KeycardReader.Admin:GetDescendants()) do
	if v:IsA("TextLabel") then else
		if v:IsA("SurfaceGui") then v.Enabled = false else
			pcall(function()
				v.Transparency = 1
			end)
		end
	end
end

--when the employee puts their keycard in the zone it hides their keycard and shows the animated keycard then animates it
--db only works the first time, every event after that has no debounce
script.Parent.KeycardReader.HitPart.Touched:Connect(function(hit)
	if not hit:FindFirstAncestor("AdministrationKeycard") or hit.Name ~= "AdministrationKeycard" or not hit:FindFirstAncestor("SecurityKeycard") or hit.Name ~= "SecurityKeycard" then
		if hit.Name == "EmployeesKeycard" or hit:FindFirstAncestor("EmployeesKeycard") then
			if db == true then return end
			db = true
			for i,v in pairs(hit:FindFirstAncestor("EmployeesKeycard"):GetDescendants()) do
				if v:IsA("TextLabel") then else
					if v:IsA("SurfaceGui") then v.Enabled = false else
						if v.Name == "Invis" then else
							pcall(function()
								v.Transparency = 1
							end)
						end
					end
				end
			end
			for i,v in pairs(script.Parent.KeycardReader.Employee:GetDescendants()) do
				if v:IsA("TextLabel") then else
					if v:IsA("SurfaceGui") then v.Enabled = true else
						if v.Name == "Invis" then else
							pcall(function()
								v.Transparency = 0
							end)
						end
					end
				end
			end
			tweenmodule.TweenModel(script.Parent.KeycardReader.Employee,script.Parent.KeycardReader.KeycardIn.CFrame,keycardtweeninfo)
			wait(0.5)
			script.Parent.KeycardReader.LED.Keycard_Denied:Play()
			tweenmodule.TweenModel(script.Parent.KeycardReader.Employee,script.Parent.KeycardReader.KeycardOut.CFrame,keycardtweeninfo)
			wait(0.2)
			script.Parent.KeycardReader.LED.BrickColor = BrickColor.new("Bright red")
			script.Parent.KeycardReader.LED.Material = Enum.Material.Neon
			wait(0.2)
			script.Parent.KeycardReader.LED.BrickColor = BrickColor.new("Black")
			script.Parent.KeycardReader.LED.Material = Enum.Material.Metal
			wait(0.2)
			script.Parent.KeycardReader.LED.BrickColor = BrickColor.new("Bright red")
			script.Parent.KeycardReader.LED.Material = Enum.Material.Neon
			wait(0.2)
			script.Parent.KeycardReader.LED.BrickColor = BrickColor.new("Black")
			script.Parent.KeycardReader.LED.Material = Enum.Material.Metal
			
			for i,v in pairs(hit:FindFirstAncestor("EmployeesKeycard"):GetDescendants()) do
				if v:IsA("TextLabel") then else
					if v:IsA("SurfaceGui") then v.Enabled = true else
						if v.Name == "Invis" then else
							pcall(function()
								v.Transparency = 0
							end)
						end
					end
				end
			end
			for i,v in pairs(script.Parent.KeycardReader.Employee:GetDescendants()) do
				if v:IsA("TextLabel") then else
					if v:IsA("SurfaceGui") then v.Enabled = false else
						if v.Name == "Invis" then else
							pcall(function()
								v.Transparency = 1
							end)
						end
					end
				end
			end
		end
		wait(5)
		db = false
	end
end)

Here’s a video of what happens when I use the keycard reader:

1 Like

Easy fix, put db = false inside the if hit.Name == "EmployeesKeycard" or hit:FindFirstAncestor("EmployeesKeycard") then statement instead of the if not hit:FindFirstAncestor("AdministrationKeycard") or hit.Name ~= "AdministrationKeycard" or not hit:FindFirstAncestor("SecurityKeycard") or hit.Name ~= "SecurityKeycard" then statement

script.Parent.KeycardReader.HitPart.Touched:Connect(function(hit)
	if not hit:FindFirstAncestor("AdministrationKeycard") or hit.Name ~= "AdministrationKeycard" or not hit:FindFirstAncestor("SecurityKeycard") or hit.Name ~= "SecurityKeycard" then
		if hit.Name == "EmployeesKeycard" or hit:FindFirstAncestor("EmployeesKeycard") then
			-- your code
            wait(5)
            db = false
		end
	end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.