Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make the mouse stay locked when I zoom out?

Hello, so, i've been trying to integrate this camera script into a tool, and as I was bug testing, I noticed that when I press 'o' or scroll to try and zoom out, it unlocks the mouse. Code:

tool.Equipped:Connect(function()
		plr.CameraMaxZoomDistance = 8
		camera.CameraType = Enum.CameraType.Scriptable
		is.MouseBehavior = Enum.MouseBehavior.LockCenter
		is.InputChanged:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseMovement then
				xAngle = xAngle-input.Delta.x*0.4
				--Clamp the vertical axis so it doesn't go upside down or glitch.
				yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
			end
		end)
		stepped = run.RenderStepped:Connect(function()
			local Character = plr.Character
			local rootPart = Character:FindFirstChild("HumanoidRootPart")
			if Character and rootPart then
				--Set rotated start cframe inside head
				local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

				--Set camera focus and cframe
				local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
				local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))

				camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
			end
		end)
		print("Equipped")
		equip:Play()
	end)

Is there a specific reason this is happening? (is is input service)

1 answer

✓ Accepted answer

Well, I can't guarantee that it is the most efficient way to do it. But, looking at your code, it seems like you are giving the command of locking the mouse at center only once, when the tool is equipped. One thing, you can do is to loop the locking mouse center.

-- Make sure to put it at the end of code. Bcz, the code after while loop won't execute until you break it. So, I recommend you to put it at the end of your ToolEquipped code.

while wait() do
	is.MouseBehavior = Enum.MouseBehavior.LockCenter
end

Lemme know if it helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.