Lock player zoom in place with a script

So I have a script where the input is WheelFoward. The script also works only when mouse is over a specific part. How can I make it so when the mouse is over that part the players zoom is locked in place and when the mouse doesnt hover over the player can freely zoom. Current script:

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.WheelForward:connect(function()
	if mouse.Target:FindFirstChild("Rotatable") then
		mouse.Target.Rotatable.RemoteEvent.Value:FireServer()
		print("its a roll")

	end
2 Likes

I’m not sure if this is the best method, and I haven’t tested it, but this should work. You have to constrain Player.CameraMinZoomDistance and Player.CameraMaxZoomDistance to a single number in order to prevent zooming. I’ve basically coded that to happen with your mouse targets: It sets the zoom ranges to previous values when the target isn’t “Rotatable,” which should allow normal zooming again.

local mouse = game.Players.LocalPlayer:GetMouse()
local localPlayer = game.Players.LocalPlayer
local oldZoomDistanceMin = 0  --whatever your desired default values are
local oldZoomDistanceMax = 50

mouse.WheelForward:connect(function()
	if mouse.Target:FindFirstChild("Rotatable") then
		local zoomLevel = (workspace.CurrentCamera.CFrame.Position - workspace.CurrentCamera.Focus.Position).magnitude
		oldZoomDistanceMin = localPlayer.CameraMinZoomDistance
		oldZoomDistanceMax = localPlayer.CameraMaxZoomDistance
		localPlayer.CameraMinZoomDistance = zoomLevel
		localPlayer.CameraMaxZoomDistance = zoomLevel		
		mouse.Target.Rotatable.RemoteEvent.Value:FireServer()
		print("its a roll")
	else
		localPlayer.CameraMinZoomDistance = oldZoomDistanceMin
		localPlayer.CameraMaxZoomDistance = oldZoomDistanceMax		
	end
end

Hope this works out for you!

1 Like

i almost works only problem is that the camera is forever stuck so even after I dont hover over the part i cant zoom

1 Like

I think I know what I did wrong. It’s probably overwriting the oldZoomDistanceMin variable if the mousewheel is called multiple times on a “Rotatable” target. I just need to add a boolean latch. One moment…

Okay I’ve added a boolean variable to check if the zoom is already being constrained. It will only store the zoom values if zoom hasn’t already been constrained. Without this, it would overwrite the stored zoom values and lock out zooming permanently.

local mouse = game.Players.LocalPlayer:GetMouse()
local localPlayer = game.Players.LocalPlayer
local oldZoomDistanceMin = 0  --whatever your desired default values are
local oldZoomDistanceMax = 50
local isZoomConstrained = false

mouse.WheelForward:connect(function()
	if mouse.Target:FindFirstChild("Rotatable") then
		if not isZoomConstrained then
			isZoomConstrained = true
			local zoomLevel = (workspace.CurrentCamera.CFrame.Position - workspace.CurrentCamera.Focus.Position).magnitude
			oldZoomDistanceMin = localPlayer.CameraMinZoomDistance
			oldZoomDistanceMax = localPlayer.CameraMaxZoomDistance
			localPlayer.CameraMinZoomDistance = zoomLevel
			localPlayer.CameraMaxZoomDistance = zoomLevel	
		end
		mouse.Target.Rotatable.RemoteEvent.Value:FireServer()
		print("its a roll")
	else
		localPlayer.CameraMinZoomDistance = oldZoomDistanceMin
		localPlayer.CameraMaxZoomDistance = oldZoomDistanceMax
		isZoomConstrained = false		
	end
end
1 Like

works like butter,thank you very much good sir

1 Like

It was pure luck that I was working on camera zooming just before I saw your post. You’re welcome, and good luck!

1 Like