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.
How do I lock the players mouse in the center of their screen?
3 answers
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.
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)
6 comments
I tried that script, but where do I put it? Is it a LocalScript? A regular script?
Local your manipulating the mouse
yeah, but where do I put it?
StarterCharacterScripts
game.StarterPlayer.StarterCharacterScripts.LocalScript
Local scripts wont run inside the character
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
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Thank you so much!