LockCenter not working for me?

I’m trying to work on a third person script the turns on once the round has begun. I used a youtube video and something I found on the DevForum, but I modified it to fit according to my game which was probably a mistake since I’m really new to scripting. These are the three scripts I used:

LockCenter

local UIP = game:GetService("UserInputService")
local RunS = game:GetService("RunService")
local switchThird = game.ReplicatedStorage:FindFirstChild("SwitchThird")

switchThird.OnClientEvent:Connect(function(disabled)
	if disabled == true then
		print("true")
		wait(0.5)
		RunS.RenderStepped:Connect(function()
			UIP.MouseBehavior = Enum.MouseBehavior.LockCenter
		end)
	else
		print("false")
		UIP.MouseBehavior = Enum.MouseBehavior.Default
	end
end)

ThirdPersonInput

-- Services --
local UIP = game:GetService("UserInputService")
local RunS = game:GetService("RunService")
local switchThird = game.ReplicatedStorage:FindFirstChild("SwitchThird")
-- Player -- 
local localPlayer = game:GetService("Players").LocalPlayer

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

-- Starting Values --
local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(2.5, 0, 7)

-- Function -- 
switchThird.OnClientEvent:Connect(function(bool)
	print(bool)
	if bool == true then
		
		wait(1)
		camera.CameraType = Enum.CameraType.Scriptable
		
		UIP.InputChanged:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseMovement then
				xAngle = xAngle - input.Delta.x * 0.4
				-- Clamp so it doesn't invert or glitch --
				yAngle = math.clamp(yAngle - input.Delta.y * 0.4, -40, 25)
			end
		end)
					
			RunS.RenderStepped:Connect(function()
				
				local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
				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)
	else
		print("Didnt run")
		camera.CameraType = Enum.CameraType.Custom
	end
end)

ThirdPersonRotate

local camera = game:GetService("Workspace").CurrentCamera
local localPlayer = game:GetService("Players").LocalPlayer
local RunS = game:GetService("RunService")


local switchThird = game.ReplicatedStorage:FindFirstChild("SwitchThird")

switchThird.OnClientEvent:Connect(function(disabled)
	print(disabled)
	if disabled == true then
		if localPlayer:FindFirstChild("InGame") then
			local inGameBool = localPlayer:WaitForChild("InGame")
			print(inGameBool)
			if inGameBool.Value == true then
				RunS.RenderStepped:Connect(function()
					if script.Parent then -- Character
						local rootPart = script.Parent:WaitForChild("HumanoidRootPart")
						if rootPart then
							rootPart.CFrame = CFrame.new(rootPart.CFrame.p, rootPart.CFrame.p + Vector3.new(camera.CFrame.LookVector.X, 0, camera.CFrame.LookVector.Z))
						end
					end
				end)
			end
		end
	else
		print("Didnt run")
	end
end)

The “switchThird” remote event I have in here just returns a bool value based on whether the character is in game or not.

The LockCenter and the ThirdPersonInput scripts are both in StarterPlayerScripts while ThirdPersonRotate is in StarterCharacterScripts

Here is a picture of what I had so far:

It snaps into third person but the mouse does not lock to the center like I want it to. I am still able to move the mouse around freely.

Sorry this was long but thanks in advance for any help and for reading all the way through :sweat_smile:

1 Like