Help with camera CFrame

Hey there! I’ve been working on a gun system recently and wanted to make a load out UI, and decided to make it with viewportframes and a different camera for the viewportframes. The CFrame is supposed to be set like so:

local camOffset = Vector3.new(10, 10, 10)
cam.CFrame = CFrame.new(camOffset, [weapon primary part].Position)

however, even if I change the value of camOffset, the camera CFrame does not change. A video is linked below explaining what I mean.

https://youtu.be/1HW3ayK_IKY

camera updating script:

local camOffset = Vector3.new(-10, -10, -10)

script.Parent.ChildAdded:Connect(function(child)
	if child:IsA("Model") then
		cam.CFrame = CFrame.new(camOffset, child.PrimaryPart.Position)
		
		script.Parent.Parent.Primary.Text = child.Name

		for i, v in pairs(child.PrimaryPart:GetChildren()) do
			if v:IsA("Motor6D") or v:IsA("WeldConstraint") then
				if v.Name ~= "barrelAttachment" then
					v:Destroy()
				end
			end
		end

		for i, v in pairs(child:GetChildren()) do
			if v:IsA("BasePart") then
				local motor = Instance.new("WeldConstraint", child.PrimaryPart)
				if v.Name ~= child.PrimaryPart.Name then 
					motor.Part0 = child.PrimaryPart
					motor.Part1 = v
					motor.Name = v.Name
				end
			end
		end
		while task.wait() do
			--cam.CameraType = Enum.CameraType.Scriptable
			child:SetPrimaryPartCFrame(child.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(1), 0)) 
			local attachment = child:FindFirstChildWhichIsA("Model")
			if attachment then
				if attachment.Main:FindFirstChildWhichIsA("WeldConstraint") then
				else
					for i, v in pairs(attachment:GetChildren()) do
						if v:IsA("BasePart") then
							local weld = Instance.new("WeldConstraint", attachment.PrimaryPart)
							if v.Name ~= child.PrimaryPart.Name then 
								weld.Part0 = attachment.PrimaryPart
								weld.Part1 = v
								weld.Name = v.Name
							end
						end
					end
				end
				attachment.PrimaryPart.Position = child.PrimaryPart.Muzzle.WorldPosition
				attachment:SetPrimaryPartCFrame(attachment.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(1), 0)) 
			end
		end
	end	
end)

viewport frame camera setup:

local vpfCam1 = Instance.new("Camera", script.Parent.PrimaryViewport)
vpfCam1.Name = "vpfCam"
vpfCam1.CameraType = Enum.CameraType.Scriptable
script.Parent.PrimaryViewport.CurrentCamera = vpfCam1

local vpfCam2 = Instance.new("Camera", script.Parent.SecondaryViewport)
vpfCam2.Name = "vpfCam"
vpfCam2.CameraType = Enum.CameraType.Scriptable
script.Parent.PrimaryViewport.CurrentCamera = vpfCam2

adding weapon to viewport frame (Yes, I am using metatables):

local loadoutHUD = self.plr.PlayerGui.Loadout
self.loadoutHUD = loadoutHUD
self.MainLoadoutFrame = loadoutHUD.MainFrame

self.PrimaryViewport = self.MainLoadoutFrame.PrimaryViewport

if self.PrimaryViewport:FindFirstChildWhichIsA("Model") then
	local model = self.PrimaryViewport:FindFirstChildWhichIsA("Model")
	model:Destroy()
end

local vpfWep = wep:Clone()
vpfWep.PrimaryPart.Position = Vector3.new(0, 0, 0)
vpfWep.settings:Destroy()
vpfWep.Parent = workspace
vpfWep.Parent = self.PrimaryViewport

self.vpfWep = vpfWep

If anyone knows a solution for this, please reply! I’ve been stuck with this problem for a while now

  • CFrame.Angles(math.rad(-60), math.rad(-45), math.rad(-45))

    local cam = Instance.new(“Camera”)
    cam.Parent = viewportFrame
    cam.FieldOfView = 45
    cam.CFrame = CFrame.new(0, 0, 0)
    cam.BackgroundColor3 = Color3.fromRGB(255, 255, 255)

    local viewportFrame = Instance.new(“ViewportFrame”)
    local cam = Instance.new(“Camera”)
    viewportFrame.Parent = workshopPreview

    local function setViewportFrame(weapon, container)
    Frame = CFrame.new(camOffset, [weapon primary part].Position) * CFrame.Angles(math.rad(-60), math.rad(-45), math.rad(-45))
    olor3 = Color3.fromRGB(255, 255, 255)
1 Like

This works, but I want the weapon model to rotate.