Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do I Make The Player's Mouse Lock?

I've Searched Every To Find How To Lock the Mouse So The Player Is In ThirdPerson And Has Their Mouse Locked.

2 answers

UserInputServicehas a special function to change the behavior of the mouse.

MouseBehavior States:

Default:0 LockCenter:1 LockCurrentPosition:2

Code:

local UserInputService = game:GetService("UserInputService")

while true do --Loops due the CoreMenu resetting the state when you pause the game
	wait()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter --Changes the behavior to 'LockCenter'
end

http://wiki.roblox.com/index.php?title=API:Class/UserInputService/MouseBehavior

3 comments

0

How Do You Make It Change Back So You Can Move It

0

You could probably add a function and a variable that will be set true or false, depending whether it is true or false it would change the state.

0

My code can do this, I needed mines to do that too. I will edit my answer to suit your needs

I have a script that does this that I edited a while ago, you can use this (:

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local cam = game.Workspace.CurrentCamera
print("Ready")
cam.CameraType = Enum.CameraType.Scriptable
wait()
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter

local x = 0
local y = 0

spawn(function()
	while true do
		game:GetService("RunService").RenderStepped:wait()
		hrp.CFrame = CFrame.new(hrp.Position) * CFrame.Angles(0, x, 0)
		cam.CoordinateFrame = hrp.CFrame * CFrame.Angles(y, 0, 0) * CFrame.new(2, 2, 12)
	end
end)

function MouseMove(name, state, inputObject)
	x = x + (-inputObject.Delta.x / 100)
	y = y + (-inputObject.Delta.y / 100)
	if y > 1.5 then
		y = 1.5
	elseif y < -1.5 then
		y = -1.5
	end
end

script.Lock.Changed:connect(function()
	if script.Lock.Value == false then
		print('Mouse Unlocked')
		game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
		--cam.CameraType = Enum.CameraType.Follow
	elseif script.Lock.Value == true then
		game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
		--cam.CameraType = Enum.CameraType.Scriptable
		print('Mouse Locked')
	end 
end)

game:GetService("ContextActionService"):BindActionToInputTypes("MouseMove", MouseMove, false, Enum.UserInputType.MouseMovement)

-Just put this inside of a local script and put it in StarterPack.

-Add a BoolValue under the script and name it Lock.

-Add another LocalScript and put it under the main one and add this code

player = script.Parent.Parent.Parent
plr = game.Players.LocalPlayer
repeat wait() until plr.Character
mouse = plr:GetMouse()
local Player = plr.Character


mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "c" then
	wait(.2)
	if plr.Backpack.MouseLock.Lock.Value == true then 
		plr.Backpack.MouseLock.Lock.Value = false
	elseif plr.Backpack.MouseLock.Lock.Value == false then 
		plr.Backpack.MouseLock.Lock.Value = true

end	
end
end)

This should work perfectly. The only bug is you won't be able to move your character freely. This is the only problem I've found with my method I've been trying to fix. I hope this helps.

8 comments

0

The Script Is Kinda Long

0

What do you mean? ;-; Does it not work (I'm sure it does) or do you not understand it?

0

Yes, I have a question. How does the script work!?!

0

I'm Just A Noob Scripter I Don't Get It

0

It works by first locking your mouse, then readjusting it to the CFrame located in line 18. After that the loop basically just makes your character face whever your mouse is pointing to avoid glitching.

0

I Don't Know What That is

0

I Will Use it Then

0

If I ever fix the bug, I will give you the updated code.

This discussion is preserved as it was. New answers, comments, and votes are closed.