Centering Mouse With A Keybind

Hey! I’ve been trying to make a toggleable mouse centering script for a few days now but i havent been able to get it working correctly for some reason.I feel like the problem might be in the input part but i dont know how to fix it.Any help will be appreciated!

This is the script i have right now

local mouseCentered = 1
local UIS = game:GetService("UserInputService")
local RunService = game:GetService"RunService"

UIS.InputBegan:Connect(function(input,Chatting)
	if Chatting then return end
	if input.KeyCode == Enum.KeyCode.V then
		if mouseCentered == 1 then
			
			mouseCentered = 0
			
		end
		
		if mouseCentered == 0 then
			
			mouseCentered = 1
			
		end
	end
end)

if mouseCentered == 1 then
	
	while mouseCentered == 1 do
			UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
			RunService.RenderStepped:Wait()
			UIS.MouseIconEnabled = false
	end
end

if mouseCentered == 0 then
	
	while mouseCentered == 0 do
		UIS.MouseBehavior = Enum.MouseBehavior.Default
		RunService.RenderStepped:Wait()
		UIS.MouseIconEnabled = true
	end
end

It looks like the issue in your script might be related to how you are toggling the mouseCentered variable. Right now, your script is toggling the variable between 1 and 0 within the same if statement, which means it will only ever set it to one value at a time.

To fix this, you could use an if-else statement to properly toggle the mouseCentered variable. Here is an updated version of your script:

local mouseCentered = true
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

UIS.InputBegan:Connect(function(input, Chatting)
    if Chatting then return end
    if input.KeyCode == Enum.KeyCode.V then
        mouseCentered = not mouseCentered
    end
end)

while true do
    if mouseCentered then
        UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
        UIS.MouseIconEnabled = false
    else
        UIS.MouseBehavior = Enum.MouseBehavior.Default
        UIS.MouseIconEnabled = true
    end
    RunService.RenderStepped:Wait()
end

In this updated script, we use mouseCentered = not mouseCentered to toggle between true and false. Then, we have a continuous while true loop where we check the value of mouseCentered and set the UIS.MouseBehavior and UIS.MouseIconEnabled accordingly.

I hope this helps!

2 Likes

Really look at that and try to follow the flow of the script. mouseCentered is 1 at this moment. So this will be true: if mouseCentered == 1 then. If the V key was pressed. This will change mouseCentered from 1 to 0.

But then we hit the next if statement and that, now is also true. So mouseCentered goes back to a 1.

MRX_perte has your fix. You only make this mistake once. :slight_smile:

1 Like

i tried the script out and it mostly works but the only problem is when it toggles the default camera it wont let me rotate the camera,any idea on why that happens?

yea i probably should have used else instead of if there but i wasnt thinking straight.thanks for that though! wont make the same mistake again

You can not move the camera because it is locked to the center just like the mouse. You’ll have to move the whole body.

what do you mean by moving the whole body?

Well if the entire body turns that would move the center of the screen …
I’ve tried to get around that myself. Didn’t work out. It’s locked. I’m sure it’s locking the screen view and placing the mouse icon in the center and not really centering the mouse. idk…