"Attempt to index nil with 'SetMouseLockOffset'" - Trying to fix a Mobile Mouselock UI


The code is supposed to activate on the press of the button, and it’s purpose is to simulate Mouselock; but for mobile users.

The error occurs on line 5, & 11. CM.activeCameraController:SetMouseLockOffset(Vector3.new(1.75,0,0))
I didn’t create this code, it was given to me and it’s been broken for a while.

Local Script

local CM,UIS = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild('PlayerModule'):WaitForChild('CameraModule')),game:GetService('UserInputService')
script.Parent.Parent.Enabled = UIS.TouchEnabled 

script.Parent.MouseButton1Click:Connect(function(input,GPE)
	CM.activeCameraController:SetMouseLockOffset(Vector3.new(1.75,0,0))
	CM.activeCameraController:SetIsMouseLocked(not CM.activeCameraController:GetIsMouseLocked())
	script.Parent.ImageLabel.ImageColor3 = CM.activeCameraController:GetIsMouseLocked() and Color3.fromRGB(125,255,125) or Color3.fromRGB(45, 45, 45)
end)

script.Parent.TouchTap:Connect(function(input,GPE)
	CM.activeCameraController:SetMouseLockOffset(Vector3.new(1.75,0,0))
	CM.activeCameraController:SetIsMouseLocked(not CM.activeCameraController:GetIsMouseLocked())
	script.Parent.ImageLabel.ImageColor3 = CM.activeCameraController:GetIsMouseLocked() and Color3.fromRGB(125,255,125) or Color3.fromRGB(45, 45, 45)
end)

Any help is appreciated.

This is the result of changes made to the default camera module. Originally, the module returned an object that could be interacted with, which is what your script is doing, but at some point it was switched to return an empty table instead.

To restore the original behavior, you will need to fork the camera module. To do this, open the game and click “Run.” Under StarterPlayer.StarterPlayerScripts, you’ll find a fresh copy of the current PlayerModule that handles default player movement and camera control. Copy this, stop the game, and paste the PlayerModule back into StarterPlayer.StarterPlayerScripts.

Once you’ve pasted in the module, open the CameraModule just under the main PlayerModule. Scroll to the bottom and replace the empty table in the return line to the cameraModuleObject created just above it. This should allow your system to communicate with the camera module again, although I can’t guarantee it’ll have the same behavior as before.

Bear in mind that forking the PlayerModule will prevent it from auto-updating, so if there are other changes in the future that you want to take advantage of, you will need to manually port them over.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.