Light gets stuck when I rapid press a key

I’ve been trying to fix this issue for awhile and it seems like no matter what I do it always happens


The effect can be seen at the end of this video
the light stays on even when I am not holding the key

Heres the entire lua file

local mouse = game.Players.LocalPlayer:GetMouse()
local camera = workspace.Cameras
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local on = false
local flash = true
game.ReplicatedStorage.ToClient.OnClientEvent:Connect(function(reason,args)
	if reason == "DISABLEFLASH" then
		print("WHYYYY")
		flash = false
		for i = 0,12 do
			--workspace["Cam"..i].Part.SurfaceLight.Enabled = false;
			workspace["Cam"..i].Part.SurfaceLight.Brightness = 0;
		end
		game.ReplicatedStorage.ToServer:FireServer("NBAT")
		workspace.Map.flash.Light.On:FireServer("TURNOFFplease")
	end
end)
function ok(toggle)
	print(flash)
	if toggle == "ON" and on == false then
		on = true
		if flash == true and game.ReplicatedStorage.get:InvokeServer("MASK") ~= true then
			if camera.Value == 0  then
			workspace.Map.flash.Light.On:FireServer("TURNONbutnotlikethat")
			game.ReplicatedStorage.ToServer:FireServer("SBAT")
		else
			game.ReplicatedStorage.ToServer:FireServer("SBAT")
			workspace["Cam"..camera.Value].Part.SurfaceLight.Brightness = 4
			end
		end
	elseif toggle == "OFF" and on == true then
		workspace.Map.flash.Light.On:FireServer("TURNOFFplease")
		game.ReplicatedStorage.ToServer:FireServer("NBAT")
		if camera.Value ~= 0  then
		workspace["Cam"..camera.Value].Part.SurfaceLight.Brightness = 0
		end
		on = false
	end
end
--[[mouse.KeyDown:Connect(function(key)
	if key:byte() == 50 then 
	if flash == true and game.ReplicatedStorage.get:InvokeServer("MASK") ~= true then
		if camera.Value == 0  then
			workspace.Map.flash.Light.On:FireServer("TURNONbutnotlikethat")
			game.ReplicatedStorage.ToServer:FireServer("SBAT")
		else
			game.ReplicatedStorage.ToServer:FireServer("SBAT")
			workspace["Cam"..camera.Value].Part.SurfaceLight.Brightness = 4
			end
		end
	else
	workspace["Cam"..camera.Value].Part.SurfaceLight.Brightness = 0
	end
end)
mouse.KeyUp:Connect(function(key)
	if key:byte() == 50 then
		workspace.Map.flash.Light.On:FireServer("TURNOFFplease")
		game.ReplicatedStorage.ToServer:FireServer("NBAT")
		if camera.Value ~= 0  then
		workspace["Cam"..camera.Value].Part.SurfaceLight.Brightness = 0
		end
	end
end)]]
local UserInputService = game:GetService("UserInputService")
function f(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		ok("ON");
	elseif inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel or inputState == Enum.UserInputState.None then
		ok("OFF");
	end
end
CAS:BindAction("FLASH",f,true,Enum.KeyCode.LeftControl,Enum.KeyCode.Z);
CAS:SetTitle("FLASH","Flash")
-- A sample function providing multiple usage cases for various types of user input
--[[UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftControl then
			ok("ON");
		end
	end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftControl then
			ok("OFF");
		end
	end
end)]]

--[[game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(pressed)
	if pressed:byte() == 50  then
		ok("ON");
	end
end)
game.Players.LocalPlayer:GetMouse().KeyUp:Connect(function(pressed)
	if pressed:byte() == 50 then
		ok("OFF")
	end
end)]]

Add a wait() maybe? If that doesn’t work I’m not really sure