Only works the first time I press it

Hello developers. So I made a script were if you press a bind the camera would focus on the ball that’s currently in air. But when I press Bind 1 on the keyboard It does it perfectly fine. But when I press the bind again to get out of focus mode it stays on focus mode. How can I fix this?

Code:

local UIS = game:GetService('UserInputService')
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local UsingCam = false
local Distance = -1
local Ball = nil

workspace.CurrentCamera.CameraType = 'Custom'

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.One then
		UsingCam = true
		local Carrier = Instance.new('Folder')
		Carrier.Name = 'Carrier'
		for _,V in pairs(workspace:GetDescendants()) do
			if V.Name == 'Football' then
				if V:IsA('Tool') then
					local Handle = V:WaitForChild('Handle')
					if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude < Distance then
						Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
						Ball = V
						workspace.CurrentCamera.CameraSubject = V
					end
				else
					if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude < Distance then
						Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
						Ball = V
						workspace.CurrentCamera.CameraSubject = V
					end
				end
			end
		end
	end
end)

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.One then
		if Player.Backpack:FindFirstChild('Carrier') then
			Player.Backpack.Carrier:Destroy()
		end
		UsingCam = false
		Ball = nil
		Distance = -1
		workspace.CurrentCamera.CameraSubject = Player.Character.Humanoid
	end
end)

workspace.DescendantAdded:Connect(function(Child)
	if UsingCam then
		if Ball == nil then
			if Child.Name == 'Football' then
				if Child:IsA('Tool') then
					local Handle = Child:WaitForChild('Handle')
					Ball = Handle
					workspace.CurrentCamera.CameraSubject = Handle
				else
					Ball = Child
					workspace.CurrentCamera.CameraSubject = Child
				end
			end
		end
	end
end)

workspace.DescendantRemoving:Connect(function(DEC)
	if UsingCam then
		if DEC == Ball then
			for _,V in pairs(workspace:GetDescendants()) do
				if V.Name == 'Football' then
					if V:IsA('Tool') then
						local Handle = V:WaitForChild('Handle')
						if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude < Distance  then
							Distance = (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude
							Ball = Handle
							workspace.CurrentCamera.CameraSubject = V.Handle
						end
					else
						if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude < Distance then
							Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
							Ball = V
							workspace.CurrentCamera.CameraSubject = V
						end
					end
				end
			end
		end
	end
end)

workspace.ChildRemoved:Connect(function(Child)
	if UsingCam then
		if Child == Ball then
			Ball = nil
		end
	end
end)
1 Like

You don’t need to make 2 Inputs,make it in one Input only,use debounce to check if the key is pressed and the UsingCam == true,if is True then get out of focus mode,else then make the camera set to focus mode.

2 Likes
local UIS = game:GetService('UserInputService')
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local UsingCam = false
local Distance = -1
local Ball = nil

workspace.CurrentCamera.CameraType = 'Custom'

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.One then
		if not UsingCam then
			
			UsingCam = true
			local Carrier = Instance.new('Folder')
			Carrier.Name = 'Carrier'
			for _,V in pairs(workspace:GetDescendants()) do
				if V.Name == 'Football' then
					if V:IsA('Tool') then
						local Handle = V:WaitForChild('Handle')
						if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude < Distance then
							Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
							Ball = V
							workspace.CurrentCamera.CameraSubject = V
						end
					else
						if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude < Distance then
							Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
							Ball = V
							workspace.CurrentCamera.CameraSubject = V
						end
					end
				end
			end
			
		else
			if Player.Backpack:FindFirstChild('Carrier') then
				Player.Backpack.Carrier:Destroy()
			end
			UsingCam = false
			Ball = nil
			Distance = -1
			workspace.CurrentCamera.CameraSubject = Player.Character.Humanoid
		end
	end
end)

workspace.DescendantAdded:Connect(function(Child)
	if UsingCam then
		if Ball == nil then
			if Child.Name == 'Football' then
				if Child:IsA('Tool') then
					local Handle = Child:WaitForChild('Handle')
					Ball = Handle
					workspace.CurrentCamera.CameraSubject = Handle
				else
					Ball = Child
					workspace.CurrentCamera.CameraSubject = Child
				end
			end
		end
	end
end)

workspace.DescendantRemoving:Connect(function(DEC)
	if UsingCam then
		if DEC == Ball then
			for _,V in pairs(workspace:GetDescendants()) do
				if V.Name == 'Football' then
					if V:IsA('Tool') then
						local Handle = V:WaitForChild('Handle')
						if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude < Distance  then
							Distance = (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude
							Ball = Handle
							workspace.CurrentCamera.CameraSubject = V.Handle
						end
					else
						if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude < Distance then
							Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
							Ball = V
							workspace.CurrentCamera.CameraSubject = V
						end
					end
				end
			end
		end
	end
end)

workspace.ChildRemoved:Connect(function(Child)
	if UsingCam then
		if Child == Ball then
			Ball = nil
		end
	end
end)
2 Likes

Nice,that’s what i’m talking about.

Lmao I found that funny for some reason