How to make a keycode for the player's camera distance?

  1. What do you want to achieve? I wanna make it so the player’s camera (like third person or first person) changes when a keycode is pressed.

  2. What is the issue?

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local key = Enum.KeyCode.Q
local tpdistance = 5
local fpdistance = 0.5

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
		if input.KeyCode == key then
			player.CameraMinZoomDistance = tpdistance
			player.CameraMaxZoomDistance = tpdistance
		else
			player.CameraMinZoomDistance = fpdistance
			player.CameraMaxZoomDistance = fpdistance
	end
end)

It works but the problem is it only works when i randomly press something on my keyboard or after a couple of seconds

  1. What solutions have you tried so far? Nothing. I just figured it out myself, I haven’t searched anything about it yet

Are you trying to make a toggle first of all?

Yes, by pressing a keycode on the keyboard

You can do this, your issue is you are doing (if inputkeycode equal to key then do something, that is correct HOWEVER you put in else which is (if inputkeycode is not equal then it will do whatever), this is not making a toggle

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local key = Enum.KeyCode.Q
local toggle = false
local tpdistance = 5
local fpdistance = 0.5

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == key then
		if not toggle then
			player.CameraMinZoomDistance = tpdistance
			player.CameraMaxZoomDistance = tpdistance
		else
			player.CameraMinZoomDistance = fpdistance
			player.CameraMaxZoomDistance = fpdistance
		end
		toggle = not toggle
	end
end)