Keybind FOV changer script

Im sure some sort of simulation game out there is in need of an FOV changer for taking pretty photos to hang on your wall. Most games I’ve seen use a free model FOV slider from the toolbox which is annoying to control because you can only change it in third person since your mouse is locked in first person.

I have a solution!

This script decreases your FOV when you hold “Z” and increases it when you hold “X”

There is also a text label that displays your FOV however this is completely optional.

Script:

local UserInputService = game:GetService("UserInputService")

local localPlayer = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local FOVLabel = script.Parent
local maxFov = 70
local minFov = 20

local function updateFOVLabel()
	if UserInputService:IsKeyDown(Enum.KeyCode.Z) or UserInputService:IsKeyDown(Enum.KeyCode.X) then
		FOVLabel.Visible = true
		FOVLabel.Text = "FOV: ".. math.floor(camera.FieldOfView)
	else
		FOVLabel.Visible = false
	end
end

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		while task.wait() and input.UserInputState == Enum.UserInputState.Begin do
			camera.FieldOfView = camera.FieldOfView - 0.75
			if camera.FieldOfView <= minFov then
				camera.FieldOfView = minFov
				break
			end
			updateFOVLabel()
		end
	elseif input.KeyCode == Enum.KeyCode.X then
		while task.wait() and input.UserInputState == Enum.UserInputState.Begin do
			camera.FieldOfView = camera.FieldOfView + 0.75
			if camera.FieldOfView >= maxFov then
				camera.FieldOfView = maxFov
				break
			end
			updateFOVLabel()
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z or input.KeyCode == Enum.KeyCode.X then
		updateFOVLabel()
	end
end)

No text label version:

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local maxFov = 70
local minFov = 20

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		while task.wait() and input.UserInputState == Enum.UserInputState.Begin do
			camera.FieldOfView = camera.FieldOfView - 0.75
			if camera.FieldOfView <= minFov then
				camera.FieldOfView = minFov
				break
			end
		end
	elseif input.KeyCode == Enum.KeyCode.X then
		while task.wait() and input.UserInputState == Enum.UserInputState.Begin do
			camera.FieldOfView = camera.FieldOfView + 0.75
			if camera.FieldOfView >= maxFov then
				camera.FieldOfView = maxFov
				break
			end
		end
	end
end)

Example of how you should set it out:
image

1 Like

what about the no text label version?

Some suggestions I can provide:

  • Use task.wait() in favor of wait() to avoid throttling.
  • You should do the checking of FOV exceeding the max FOV before setting the FOV. Your current code makes it so that the FOV set exceeds the max FOV, and when the conditional statement detects this, it will set back to the max FOV, which makes your FOV looks shaky and buggy when you keep holding the X key. This goes the same for decreasing FOV.

Overall, this is good. Preferably, you can make this as a model, and show some examples of your script working.

1 Like

Just put the script in StarterGUI

1 Like