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
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
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)
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
)
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
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()