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

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

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

✓ Accepted answer

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.

1 comment

0

Thank you so much!

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

0

I tried that script, but where do I put it? Is it a LocalScript? A regular script?

0

Local your manipulating the mouse

0

yeah, but where do I put it?

0

StarterCharacterScripts

0

game.StarterPlayer.StarterCharacterScripts.LocalScript

0

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.