CameraMaxZoomDistance not Working

I’m trying to make the camera stay still at the position of a part. I don’t want the player to be able to zoom, so I set CameraMaxZoomDistance to 0.5. I don’t know why, but that didn’t work. The CameraMaxZoomDistance stayed the same, just as if i had never written that line of code. However, when I run the game in Studio and change my CameraMaxZoomDistance through the explorer, it works. It also works when the game is run and I change CameraMaxZoomDistance through the command bar. I don’t know if this is a bug or just me not doing it right, so that’s why it’s not a bug report yet.

Here is my code:

local plr = game:GetService('Players').LocalPlayer
local cam = workspace.CurrentCamera
local campart = workspace:WaitForChild('CamPart')

cam.CameraType = Enum.CameraType.Fixed
cam.Focus = campart.CFrame
plr.CameraMinZoomDistance = 0.5
plr.CameraMaxZoomDistance = 0.5
wait()
print(plr.CameraMaxZoomDistance) --This always prints "128", the default max zoom
3 Likes

I also have game.Players.CharacterAutoLoads set to false, but that didn’t effect the script

Try replacing that code (just comment it out) with this, as a test:

local player = game.Players.LocalPlayer
 
player.CameraMaxZoomDistance = 0.5
player.CameraMinZoomDistance = 0.5

print(player.CameraMaxZoomDistance)

It’s just the same thing you’re doing but simplified to just include the part that changes the distance, just as a test. I got it from the roblox api: Player | Documentation - Roblox Creator Hub

Are there any errors?

No errors.
It printed 0.5, but I could still zoom in and out. On the explorer the max zoom still said 128.

I’m not familiar with zoom distance, but is it a value for humanoid, not player?

Is this a LocalScript? And where are you placing it?

It’s a property of Player, not Humanoid. Player | Documentation - Roblox Creator Hub

It’s a property of player

Yes, and it’s in StarterPlayerScripts which is technically in the player’s PlayerScripts because it gets replicated.

Also, when I put the print(player.CameraMaxZoomDistance) in a while loop,
(code here:

local player = game.Players.LocalPlayer
 
player.CameraMaxZoomDistance = 0.5
player.CameraMinZoomDistance = 0.5

while true do
	print(player.CameraMaxZoomDistance)
	wait()
end

)

it prints 0.5 once then prints 128 over and over again, or it doesn’t even print 0.5 in the beginning at all. (Mostly the former)

Edit: They can be floating points.

Just as a quick test, try switching CameraMaxZoomDistance and CameraMinZoomDistance. Make it this:

local player = game.Players.LocalPlayer
 
player.CameraMinZoomDistance = 0.5
player.CameraMaxZoomDistance = 0.5

while true do
	print(player.CameraMaxZoomDistance)
	wait()
end
)

It may work, but actually they’re float values.

EDIT: Read here: Player | Documentation - Roblox Creator Hub

Ah okay, I saw your edit. Try making it higher than 0.(something) like he said, that makes more sense.

Yeah that should work. I’m testing it.

I put this code in, but the results are the same as in post#9

Ah, I just found the fix while playing around with it!

It looks like somethings going wrong with the timing of when LocalPlayer spawns in. It’s very odd, but that seems to be the problem. So the fix is simply to put a very short wait() at the beginning of the script. I did this:

local player = game.Players.LocalPlayer

wait(0.00001)

player.CameraMinZoomDistance = 1
player.CameraMaxZoomDistance = 1

while true do
	print(player.CameraMaxZoomDistance)
	wait()
end

Hope it works for you too!

3 Likes

There is no point in doing wait(0.00001, as the absolute minimum waittime is 0.033. Though, you can do renderstepped or heartbeat:wait() instead, which is much quicker. Let me show you:

local rs = game:GetService("RunService")
rs.Heartbeat:Wait()
3 Likes

Using wait() worked, so I went with that. Thanks @thoraq for your suggestion.