Camera Module help?

I’m trying to edit the CameraModule to simplify the code to do what I need it to do, which is follow the character with a specific rotation and offset, however I cannot seem to figure out why the Module’s :Update() function isn’t firing every frame, despite the rest of the code working as expected. Code and file is provided below

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Utilities = require(ReplicatedStorage:WaitForChild("Utilities"))


local Camera = game.Workspace.CurrentCamera
local CameraOffset = Vector3.new(0,0, 10)
local CameraRotation = Vector3.new(90, 0, 0)


local Character

local CameraModule = {}
CameraModule.__index = CameraModule

function CameraModule.new()
	local self = setmetatable({},CameraModule)
	
	self.CharacterAdded = Utilities.CreateNewEvent("CharacterAdded") -- Instance
	self.CharacterAdded:Connect(function(NewCharacter)
		Character = NewCharacter
		print(NewCharacter, typeof(NewCharacter))
	end)
	
	return self
end

function CameraModule:Update(TimeElapsed)
	print(1, TimeElapsed)
	if Character and Character.HumanoidRootPart ~= nil then
		local HumanoidRootPart = Character.HumanoidRootPart
		local CF = CFrame.new(HumanoidRootPart.Position + CameraOffset)
		CF = CF * CameraRotation
		Camera.CFrame = Camera.CFrame:Lerp(CF, TimeElapsed)
	end
end

return CameraModule.new()

TPS.rbxl (261.7 KB)

Looking through the default CameraModule class, it looks like you deleted the BindToRenderStep in the CameraModule.new() function.
Add:
RunService:BindToRenderStep("cameraRenderUpdate", Enum.RenderPriority.Camera.Value, function(dt) self:Update(dt) end) to your .new() function

Side note: Your CameraRotation variable is a Vector3, but I think you intended it to be a CFrame. In your Update() function you perform

CF = CF * CameraRotation -- Multiplying a CF by a Vec3 returns a Vec3
Camera.CFrame = Camera.CFrame:Lerp(CF, TimeElapsed) -- Error: Unable to cast Vector3 to CoordinateFrame
1 Like