Why doesn't this script lock the mouse?

  1. What do you want to achieve? I’m trying to get the mouse locked to the center of the script when the script begins (InputBegan), and then back to normal once it ends (InputEnded).

  2. What is the issue? (this is all done in a local script) I set the mouse to UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter but when I go in to test it, the mouse just moves to the lower part of the screen and the mouse doesn’t lock to the middle
    here’s a video of what happens

  3. What solutions have you tried so far? I’ve looked at the MouseBehavior API and topics similar to mine but none have provided the right solutions

here’s the full script:

-- services, variables, and functions
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local debounce = false
local ShieldOn = ReplicatedStorage.WandaRemotes:FindFirstChild("WandaShieldOn")
local ShieldOff = ReplicatedStorage.WandaRemotes:FindFirstChild("WandaShieldOff")

-- local animation = Instance.new("Animation")
-- animation.AnimationId = "rbxassetid://"

-- local anim = character.Humanoid:LoadAnimation(animation)

local leftGlow = character.LeftHand.Attachment
local rightGlow = character.RightHand.Attachment

local function toggle(value)
	leftGlow.a.Enabled = value
	leftGlow.b.Enabled = value
	leftGlow.c.Enabled = value
	rightGlow.a.Enabled = value
	rightGlow.b.Enabled = value
	rightGlow.c.Enabled = value

-- code
UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C and debounce == false then
		debounce = true
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter -- the part of the script that doesnt work

		-- anim:Play()

		debounce = false

UserInputService.InputEnded:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.C then
		-- anim:Stop()

all help is appreciated!

This property doesn’t lock the mouse permanently, it just moves it to the center, but doesn’t lock it there. What you can do is use a RenderStepped event to constantly update its position, then when you don’t need it anymore, disconnect it.

connection = RunService.RenderStepped:Connect(function()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

1 Like