Forcing Mouselock with new Camera Script

Hi! I’m trying to force a mouse lock with a custom camera script. I was able to do it with the previous camera script but now since the newer(and better might I add), camera script was introduced I’m not able to do it. Could anybody help?

Essentially what I need is whenever a value(bool value) is updated/changed the check mouselock function runs. This way I can choose when a player has to have a mouselock or not, I don’t want them to determine when they want to be able to use it. I’ve sort of got a start on it, there’s the code below. I got too confused after this point because I’m new to to this cam system. Thanks!

MouseLockController.rbxm (4.1 KB)

3 Likes

I’m a bit confused by what you’re asking for. Are you trying to make it so it locks in first person? or some sort of mouse lock in a third person perspective.

Imagine forcing shift lock, so instead of the user pressing shift you(the scripter) can toggle it with your own code via a book value in the humanoid

I’m assuming you don’t want to set it using the core camera script settings, right?

You can use MouseBehavior for this. The code should be placed in a LocalScript .

local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
-- locks mouse in the center of the screen

To revert it back:

UserInputService.MouseBehavior = Enum.MouseBehavior.Default
-- mouse moves freely around the screen.
9 Likes

I do

I had to do this on RenderStepped as I believe the camera script set it back every frame.

I ended up using the following:

local UserGameSettings = UserSettings( ):GetService("UserGameSettings")

local Event, Original

function ForceShiftLock( )
    _, Original = pcall( function ( ) return UserGameSettings.RotationType end )
    Event = game:GetService( "RunService" ).RenderStepped:Connect( function ( ) pcall( function ( ) UserGameSettings.RotationType = Enum.RotationType.CameraRelative end ) end )
end

function EndForceShiftLock( )
    if Event then
        pcall( function ( ) UserGameSettings.RotationType = Original or Enum.RotationType.MovementRelative end ) 
        Event:Disconnect( )
    end
end
5 Likes

Placing this line of code in the beginning of a Local Script will not work as MouseBehavior is probably being changed by the default camera scripts.

local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

You should instead place it in a loop or in a function:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	end
end)

My code already has them being set in a RenderStepped event

Hold up. Is OP looking for mouse lock or shift lock?

Looks like shift lock, which is what the code I provided does :slight_smile:

1 Like

So if you want to do this a simple way is to run the game in studio and find the camera script, then copy it, and end the game. Then paste it back to where it was found and go into the script and find the camera settings area, it shouldnt be too hard to find. Then there will be a list of variable setting like “ThirdPersonLocked” and things like that. and then the variable for your camera setting to true and that should work

1 Like