Why is my debounce not working?

I’m pretty new at scripting and I can’t figure this out. I’m trying to make a debounce script that changes the Camera type and sets it to the c-frame of a part. It acts kind of like a security camera. It turns on, however, it won’t go back off. Any ideas why it’s not working?

Local Script:

local Tool = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character
local cam = game.Workspace.CurrentCamera
local Part = script.PartTrag.Value

debounce = false

script.Parent.MouseButton1Click:Connect(function()
	if debounce == false then
		if cam.CameraType == Enum.CameraType.Custom then
			repeat wait()
				cam.CameraType = Enum.CameraType.Scriptable
			until cam.CameraType == Enum.CameraType.Scriptable
			cam.CFrame = Part.CFrame
			cam.FieldOfView = 40
			script.Parent.BackgroundColor3 = Color3.new(0, 1, 0.498039)
			print("ActivatedStage")
			debounce = true
		else
			cam.CameraType = Enum.CameraType.Custom
			cam.FieldOfView = 70
			script.Parent.BackgroundColor3 = Color3.new(1, 0.701961, 0)
			print("DectivatedStage")
			debounce = false
		end
	end
end)
1 Like

Your else statement is in both if statements. You have to put it after the first end at the bottom.

debounce = false
script.Parent.MouseButton1Click:Connect(function()
	if debounce == false then
        debounce = true
		if cam.CameraType == Enum.CameraType.Custom then
			repeat wait()
				cam.CameraType = Enum.CameraType.Scriptable
			until cam.CameraType == Enum.CameraType.Scriptable
			cam.CFrame = Part.CFrame
			cam.FieldOfView = 40
			script.Parent.BackgroundColor3 = Color3.new(0, 1, 0.498039)
			print("ActivatedStage")
		end
    elseif debounce == true then
        debounce = false
        if cam.CameraType == Enum.CameraType.Scriptable then
			cam.CameraType = Enum.CameraType.Custom
			cam.FieldOfView = 70
			script.Parent.BackgroundColor3 = Color3.new(1, 0.701961, 0)
			print("DectivatedStage")
		end
	end
end)
1 Like

Thank you so much! It works now

2 Likes

Also, you should put the debounce below the if statements to prevent errors and debounce activation might messed up. I just forgot to do it.

1 Like