Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Do I Make The Player's Mouse Lock? [closed]

Asked by 8 years ago

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

Closed as Not Constructive by Link150, User#11440, and TheHospitalDev

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

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

0
How Do You Make It Change Back So You Can Move It MentalHealthIssues 5 — 8y
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. UniversalDreams 205 — 8y
0
My code can do this, I needed mines to do that too. I will edit my answer to suit your needs StoIid 364 — 8y
Ad
Log in to vote
0
Answered by
StoIid 364 Moderation Voter
8 years ago
Edited 8 years ago

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.

0
The Script Is Kinda Long MentalHealthIssues 5 — 8y
0
What do you mean? ;-; Does it not work (I'm sure it does) or do you not understand it? StoIid 364 — 8y
0
Yes, I have a question. How does the script work!?! User#11440 120 — 8y
0
I'm Just A Noob Scripter I Don't Get It MentalHealthIssues 5 — 8y
View all comments (4 more)
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. StoIid 364 — 8y
0
I Don't Know What That is MentalHealthIssues 5 — 8y
0
I Will Use it Then MentalHealthIssues 5 — 8y
0
If I ever fix the bug, I will give you the updated code. StoIid 364 — 8y