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

How do I lock the players mouse in the center of their screen?

Asked by 5 years ago
Edited 5 years ago

I've been looking everywhere for this. I'm going to make a horror game where it's always first person and your mouse is in the middle of your screen and it's a white dot. I really, really want to know how to lock my mouse in the center of my screen. If you see this and know the answer, please don't ignore if you know the answer.

3 answers

Log in to vote
0
Answered by
WoTrox 345 Moderation Voter
5 years ago

You don't need to code that much. Go to StarterPlayer in the Explorer, click on it, in the Properties scroll down to CameraMode and change it to LockFirstPerson. Write this inside a LocalScript then put it in the StarterCharacterScripts inside the StarterPlayer:

local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
mouse.Icon = "rbxassetid://3504534052"

Change the ID (3504534052) to the Decal's ID that you want to make as the cursor. If you uploaded a Decal and you had changed the ID to your's, but when you're playing it's invisible or says failed to load, wait a few days, and try again.

0
Thank you so much! Ryanrecker2 2 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

Remember to use google as well

-------------
--- INDEX ---
-------------

--Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

--Player & Character
local localPlayer = game:GetService("Players").LocalPlayer

--Other
local Camera = game:GetService("Workspace").CurrentCamera

--------------
--- VALUES ---
--------------

local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(3.5,0,7.5)

----------------------
--- INITIALIZATION ---
----------------------

wait(1)
Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

-----------------
--- FUNCTIONS ---
-----------------

userInputService.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        xAngle = xAngle-input.Delta.x*0.4
        --Clamp the vertical axis so it doesn't go upside down or glitch.
        yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
    end
end)

runService.RenderStepped:Connect(function()
    local Character = localPlayer.Character
    local rootPart = Character:FindFirstChild("HumanoidRootPart")
    if Character and rootPart then
        --Set rotated start cframe inside head
        local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

        --Set camera focus and cframe
        local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
        local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))

        Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
    end
end)
0
I tried that script, but where do I put it? Is it a LocalScript? A regular script? Ryanrecker2 2 — 5y
0
Local your manipulating the mouse The_Pr0fessor 595 — 5y
0
yeah, but where do I put it? Ryanrecker2 2 — 5y
0
StarterCharacterScripts The_Pr0fessor 595 — 5y
View all comments (2 more)
0
game.StarterPlayer.StarterCharacterScripts.LocalScript GamingWithFlight 80 — 5y
0
Local scripts wont run inside the character RunKittenzRComin 170 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I'm making an fps so here is the deploy 4 me

game.StarterGui.ScreenGui.Frame.DeployButton.MouseButton1Click:Connect(function()
game.StarterPlayer.CameraMode.LockFirstPerson = true
game.StarterGui.ScreenGui.Frame.DeployButton.MouseButton1Click:Connect(function()
game:GetService("Players")
local Mouse = Players.LocalPlayer:GetMouse()
Mouse.Icon = "www.roblox.com/asset?id=316279305"

I will test this out, if it works better

StarterPlayer MaxZoomDistance will be 0.5

Answer this question