Mouse center lock and unlock

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to lock and unlock the mouse in game.

  2. What is the issue? Include screenshots / videos if possible!
    I have written the lock and unlock mouse code, but it seems to not work as I was expecting.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried inputBegan and RenderStepped as it is supposed to be used, however both of them don’t work even inputBegan. I have tried looking for similar topics but they’re not exactly what I wanted.

This is the code:

runService.RenderStepped:Connect(function()
	if viewMode == 1 or 3 then
		UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
	else
		UIS.MouseBehavior = Enum.MouseBehavior.Default
	end
end)

the MouseBehavior is not an repeated event which means it will be locked just for sec or so
u should do

repeat 
      UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
until viewMode == 2

I am not sure if this will fix your problem but you’ve got there a mistake:

if viewMode == 1 or 3 then

You should have there this:

if viewMode == 1 or viewMode == 3 then
1 Like

Oh… So that was my mistake! Thank you so much for pointing that out. It finally works now.

I definitely need to double check my codes now lol.