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!
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.
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.
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
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)
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