Get Player Zoom Level (LocalScript)

Here’s a local script that you can use if you needed to get the player’s zoom level (how far they’ve zoomed their camera away). It isn’t 100% accurate but I believe it works well enough.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local head = character:FindFirstChild("Head") or character:WaitForChild("Head")
local root = character:FindFirstChild("HumanoidRootPart") or character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera

function calculate(a : Vector3,b : Vector3)
	return (a-b).Magnitude
end

local function onCameraChanged()
	local zoomDistance = calculate(camera.CFrame.Position, head.Position)
	local roundedZoomDistance = math.round(zoomDistance)
	--[change the print to whatever you want]
	print(`Camera zoom level: {roundedZoomDistance}`)
end

camera:GetPropertyChangedSignal("CFrame"):Connect(onCameraChanged)

I had no reason to post this but I had trouble finding a script that does this. And I felt like others might need this.

4 Likes

The head position may change based on the animations playing on the humanoid, and does not contribute to the camera’s zoom level. You should instead use root.Position + Vector3.yAxis * 1.5, as this is the head location with no animation playing.

Additionally, this script would only work in StarterCharacterScripts. Could you find a way to make it work anywhere on the client-side?

Thank you, and I did consider that the head would animate (for example, jumping), but I never found a great solution. I’ll be sure to try out your method. And I believe this would work anywhere that’s client-sided. Here are the changes you requested:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local head = character:FindFirstChild("Head") or character:WaitForChild("Head")
local root = character:FindFirstChild("HumanoidRootPart") or character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera

function calculate(a : Vector3,b : Vector3)
	return (a-b).Magnitude
end

local function onCameraChanged()
	local zoomDistance = calculate(camera.CFrame.Position, root.Position + Vector3.yAxis * 1.5)
	local roundedZoomDistance = math.round(zoomDistance)
	print(`Camera zoom level: {roundedZoomDistance}`)
end

camera:GetPropertyChangedSignal("CFrame"):Connect(onCameraChanged)
1 Like

or just workspace.CurrentCamera.CFrame.Y

isnt that gonna change everytime u move ur camera

what if you want to just force set a zoom value, like say 50 and make it so the client cannot change it?

It changes between 0 - 1 based on angle up and down and then the rest is the distance. Otherwise, it’s very accurate for most use cases.

Have you considered employment?