Press Key to Zoom

I’m trying to change the players perspective with a key than change it back, like press the key and the camera zooms into lowest camera distance, press it again than it zooms back to the max camera distance. It changes the starterplayer max zoom distance but players distance in game.

Here’s my code:

local player = game.Players.LocalPlayer or game.Players.PlayerAdded.Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")

function onKeyPressed(inputObject, gameProcessEvent)
		if inputObject.KeyCode == Enum.KeyCode.V then    
		game.StarterPlayer.CameraMaxZoomDistance = 0.5                                                      
	end
end
uis.InputBegan:Connect(onKeyPressed)

Hmm, well first is this script in a local Script? I fell that would run much better.

Now, from what i’m understanding, you can try this:

local Camera = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key)
   if key.KeyCode == Enum.KeyCode.V then
      Camera.FieldOfView = 5
   end
end)

UIS.InputEnded:Connect(function(key)
   if key.KeyCode == Enum.KeyCode.V then
      Camera.FieldOfView = 70
   end
end)

Hope this helps you out. Reply if it doesn’t or I didn’t understand the situation correctly. :wink:

Edit: The Camera maximum zooming distance shouldn’t need to be modified since it’s the field of view thats increasing. Changing the field of view does give a “zooming” effect.

2 Likes

for some reason it does not work. hmm

it works for like half a second then goes back to normal

Oh, do you want the FOV to lock when you press the key? So like press it again to deactivate it?

1 Like

yes press the key it locks it then press and it deactivate.

Well if ya wanna lock it, try this:

local Camera = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local Lock = false

UIS.InputBegan:Connect(function(key)
   if key.KeyCode == Enum.KeyCode.V then
      if Lock == false then
         Camera.FieldOfView = 5
         Lock = true
      else
         Camera.FieldOfView = 70
         Lock = false
      end
   end
end)

Edit: You can also add a Debounce, to make a ‘cool down’ within the script

thats more of a scope type of zoom rather than putting the player into first person

Oh, first person. Right then the line of code needed for that is:

Game.StarterPlayer.CameraMode = Enum.CameraMode.LockFirstPerson --First person
Game.StarterPlayer.CameraMode = Enum.CameraMode.Classic -- third person

You can put it where the field of view was

im pretty sure its

game.Player.PlayerAdded:Wait()

and if its a local script you do not need the or part you just need the game.Players.LocalPlayer
so basically there is a error on the first line (you should keep output open when coding)
if its a local script use this code:

local player = game.Players.LocalPlayer or game.Players.PlayerAdded.Wait()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")

function onKeyPressed(inputObject, gameProcessEvent)
   if gameProcessEvent then return end

   if inputObject.KeyCode == Enum.KeyCode.V then    
	  game.StarterPlayer.CameraMaxZoomDistance = 0.5                                                      
   end
end
uis.InputBegan:Connect(onKeyPressed)

this script changes the StarterPlayer MaxCameraDistance rather than doing it for the client. Was that a mistake?

Hmm that very weird. I tested the line of code I mentioned, and no it is not a mistake, since the code no longer works. hmm.

I also tested the .CameraMaxZoomDistance but it also doesn’t seem to work.

But I just spotted what was wrong so now I think I have your solution:

local Camera = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local Lock = false
local Player = game.Players.LocalPlayer

UIS.InputBegan:Connect(function(key)
   if key.KeyCode == Enum.KeyCode.V then
      if Lock == false then
         Player.CameraMode = Enum.CameraMode.LockFirstPerson
         Lock = true
      else
         Player.CameraMode = Enum.CameraMode.Classic
         Player.CameraMinZoomDistance = --Number distance you want
         Lock = false
      end
   end
end)
1 Like

my bad change the game.StarterPlayer to Player

1 Like

are you familiar with camera zoom speed? I made some tweaks and it works but I want an instant transition I know it has something to do with the zoom module in camera but have no clue what to edit.