Prevent right click mouse from affecting camera bind

I’m having a problem where the player can hold right click to try dragging the camera around. I don’t want this to happen though
ezgif.com-gif-maker (35)

--// Get the default position for the camera
local function GetDefaultPosition()
	local Character = Player.Character
	if not Character then return end
	
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	if not HumanoidRootPart then return end
	
	local PlayerPosition = HumanoidRootPart.Position
	local CameraPosition = PlayerPosition + OFFSET
	
	return PlayerPosition, CameraPosition
end

--// Update camera movement
local function UpdateCamera()
	if CutScene then return end
	
	-- Get default position
	local PlayerPosition, CameraPosition = GetDefaultPosition()
	if not PlayerPosition or not CameraPosition then return end
	
	CurrentCamera.CFrame = CFrame.new(CameraPosition, PlayerPosition)
end

--// Start camera
function Camera.Start()
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	
	-- Bind camera
	RunService:BindToRenderStep(
		"Camera",
		Enum.RenderPriority.Camera.Value,
		UpdateCamera
	)
end
1 Like

Have you tried binding RMB to a different action? That’ll override the default camera rotation RMB usually performs.

What’s happening is that your script is initializing before the default PlayerModule control script. When the default controller does end up booting, it sets up the camera like normal even though your script will have already changed the camera to be Scriptable. This leads to the unintended consequence you are seeing.

To get around this, require()ing the control script from within your code will ensure your code doesn’t beat it to the punch. This can be done by throwing this line in near the top of your script:

require(Player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))

I’m still seeing some weird artifacts from this, though: if I run the script immediately, the camera does not jitter, but my cursor is still locked when holding right-click; however, if I yield the thread before changing the camera, this sometimes prevents the locking. If the cursor lock is a non-issue, then you don’t really need to worry about this, but forking the PlayerModule may be required if right-click needs to be used in your game.

1 Like