CameraType = Enum.CameraType.Custom Not working

I need this ModuleScript to dynamically change between the First Person camera and the other mode, which is more like a side-scroller whenever I call module:SetCamera.

For some reason, I can’t get it to come back to a first person camera. It will just get stuck in the position it was before.

I have tried every single way on earth to change the cameratype to custom and put it on the Humanoid, but it will just get stuck in the air and not work.

-- forwardLimit = Studs between player and cam forward. If it exceeds this, camera will follow
-- horizontalLimit = Studs player can move to left/right before camera starts to follow

local module = {}

local cameraUpdate = 0
function module:SetCamera(cameraCFrame: CFrame, forwardLimit: number, horizontalLimit: number, FOV:number)
	FOV = FOV or 50
	
	print(cameraUpdate)
	cameraUpdate += 1
	print(cameraUpdate)
	local player = game.Players.LocalPlayer
	local character = player.Character
	if not character or not character.Parent then
		character = player.CharacterAdded:Wait()
	end
	local humanoid = character:WaitForChild("Humanoid")
	
	local camera = workspace.Camera
	camera.FieldOfView = FOV
	
	if cameraCFrame == "FirstPerson" then
		camera.CameraType = Enum.CameraType.Custom
		camera.CameraSubject = character.Humanoid
	else
		print(player.CameraMode)
		camera.CameraType = Enum.CameraType.Scriptable
		local currentCenterOffset, prevRightDistanceToPlr = 0, 0
		local function UpdateCamera()
			local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
			local cameraPartPosition = cameraCFrame.Position
			local vectorToPlr = humanoidRootPart.CFrame.Position - cameraPartPosition
			local rightDistanceToPlr = vectorToPlr:Dot(cameraCFrame.RightVector)
			local forwardAxis = Vector3.new(cameraCFrame.LookVector.X, 0, cameraCFrame.LookVector.Z).Unit
			local forwardDistanceToPlr = vectorToPlr:Dot(forwardAxis)

			currentCenterOffset = math.clamp(
				(currentCenterOffset + (rightDistanceToPlr - prevRightDistanceToPlr)),
				-horizontalLimit,
				horizontalLimit
			)

			local newCFrame = cameraCFrame + cameraCFrame.RightVector * rightDistanceToPlr

			if forwardDistanceToPlr > forwardLimit then
				local distance = (
					Vector2.new(newCFrame.X, newCFrame.Z) -
						Vector2.new(humanoidRootPart.Position.X, humanoidRootPart.Position.Z)
				)
				distance = Vector3.new(distance.X, 0, distance.Y)
				newCFrame = newCFrame - distance - forwardAxis * forwardLimit
			end

			camera.CFrame = newCFrame - cameraCFrame.RightVector * currentCenterOffset
			prevRightDistanceToPlr = rightDistanceToPlr
		end

		UpdateCamera() --Makes sure camera is updated before player starts walking after reset/joining

		local thisUpdate = cameraUpdate
		local connection
		connection = humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			if cameraUpdate ~= thisUpdate then print("disconnected!") connection:Disconnect() end
			local startMoveDirection = humanoid.MoveDirection
			while
				cameraUpdate == thisUpdate and
				humanoid.MoveDirection ~= Vector3.zero and
				humanoid.MoveDirection == startMoveDirection
			do
				UpdateCamera()
				task.wait() --How often the camera updates. Increase to improve performance
			end
		end)
	end
end

return module

Server script used to switch the camera:

game.Players.PlayerAdded:Connect(function(plr)
	local cameraA = workspace.CameraPartA.CFrame
	local cameraB = workspace.CameraPartB.CFrame

	local function SetCamera(player: Player, cameraCFrame: CFrame, forwardLimit: number, horizontalLimit: number, FOV: number)
		local cameraEvent = game.ReplicatedStorage.CameraEvent
		cameraEvent:FireClient(
			player,
			cameraCFrame,
			forwardLimit,
			horizontalLimit,
			FOV
		)
	end
	SetCamera(plr, cameraB, 30, 5)
	task.wait(10)
	SetCamera(plr, "FirstPerson")
end)
2 Likes

I think the reason this code is not working is not because your code is wrong, it’s because you are grabbing the camera in an invalid way. Module scripts and server scripts can’t alter the player’s camera. Only a LocalScript can do that, as it is related to the client-side. The best idea in my opinion is to just fire a remote event to a LocalScript, and manipulate the camera properties there.

2 Likes

I figured it out. The issue was that Roblox names their CoreScripts things like “CameraModule” and then never communicates it to you. I was overriding that script with this module since the names were the same, so the camera was completely broken.

Very weird on their part to not communicate to you that there’s a CoreScript called “CameraModule”. The script started working 100% fine after I just changed the name!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.