Problem with Camera script

Hi, I am attempting to create a First Person movement camera script which allows the player to move in one direction and also look around with their camera freely. The problem is that I want to make it so that the player cannot look behind their characters back. I have already achieved this to some level by clamping the xAngle variable to -90 or 90 around the front of the character torso.


local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local localPlayer = game:GetService("Players").LocalPlayer
local Camera = game:GetService("Workspace").CurrentCamera

local xAngle = 0
local yAngle = 0
local cameraPos =,0,0)

Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

local Character = localPlayer.Character
local HRP = Character:WaitForChild("HumanoidRootPart")

	if input.UserInputType == Enum.UserInputType.MouseMovement then
		xAngle = xAngle-input.Delta.x*0.4
		yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)

	local Character = localPlayer.Character
	local rootPart = Character:FindFirstChild("HumanoidRootPart")
	if Character and rootPart then
		local xa = HRP.Orientation.y - 90
		local xb = HRP.Orientation.y + 90
		xAngle = math.clamp(xAngle, xa, xb)
		local startCFrame = +,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

		local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(,cameraPos.Y,cameraPos.Z))
		local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(,cameraPos.Y,-50000))

		Camera.CFrame =,cameraFocus.p)


It looks good to begin with, you cant look behind your character which is exactly what I wanted. The problem comes when you attempt to rotate your character around your camera. For some reason when you do this and the camera will start to freak out and glitch a lot. I am not sure what is causing this or how to stop this from happening. Any help would be appreciated greatly.

it goes inside studio


I believe youre meant to put it in serverscriptservice

Sorry, I misunderstood your post. I think this will take more scripting than I’m capable of.