-
What do you want to achieve? I have a camera attached to a viewmodel in my game and im trying to use a viewportframe to constantly map the cameras vision to the screen.
-
What is the issue? To do this I used renderstepped to clone the CFrame of the camera lens to the CFrame of the viewport camera, every single frame. For some weird reason it returns the error "Attempt to index nil with ‘CFrame’. (Theres probably a way easier way to do this, if so, please direct me to a tutorial or post.)
-
What solutions have you tried so far? Ive tried looking on the dev forum but i think theres only one post that doesnt fit my current predicament as far as I know.
runservice.RenderStepped:Connect(function()
cameracam.CFrame = cam:FindFirstChild("ViewModel").camera.Handle.cameraface.CFrame
end)
(If something I say in this post might seem confusing remember to ask me to elaborate further.)
2 Likes
The error is saying that cameracam
is nil.
How are you defining cameracam
?
3 Likes
Ill just send you the entire script
(cameracam is the viewport camera that im trying to clone the CFrame of the lens onto)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local cam = workspace.Camera
local runservice = game:GetService("RunService")
local cameracam = script.Parent.ScreenGui.ViewportFrame:FindFirstChild("viewport")
local viewModel = game.ReplicatedStorage.ViewModel:Clone()
viewModel.Parent = cam
if player.Character.Humanoid.Health == 0 then
if cam:FindFirstChild("ViewModel") ~= nil then
cam.ViewModel:Destroy()
end
end
if cam:FindFirstChild("ViewModel") ~= nil then
cam.ViewModel:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(.025, -1.5, -1) * CFrame.Angles(0.5, 0, 0))
end
end)
(cameraface, if youre wondering, is the camera lens.)
This is returning nil (so “viewport” doesn’t exist in ViewportFrame. Can you please confirm that it exists, and if it does see if replacing FindFirstChild with WaitForChild to see if that fixes it
It does indeed exist

And changing FindFirstChild to WaitForChild returns with infinite yield.
Just to double check, viewport
is of type Camera, right?
That might be the issue, correct me if im wrong, but i dont see any cameratype called camera though.

I meant the object itself which, based on your image, is of type camera.
I believe the issue is that the camera object created in StarterGui does NOT get copied over into the player’s GUI. Looking online, it seems that the way to do it is to create the camera using Instance.new() from within the local script, then parenting it to the viewport
Ok ill try that and ill mark your post as the solution if it helps.
The view model is now loading which means that there are no errors in the script, but the view port frame (the frame circled in red) isnt loading the cameras vision its just a still image.
I don’t think anything in your code is telling it to change the position. Is it meant to be relative to the player’s actual camera
No the camera is supposed to follow the camera lens of the camera that the viewmodel is holding.
In your script, you should add some code that constantly updates the CFrame of the created camera to the camera lens cframe.
(is workspace.Camera the player’s camera, or is it the camera model?)
Im pretty sure this works I just forgot to delete the old camera and set the current camera of the viewportframe to the new one.
New problem has occurred, the camera lens does constantly map to my camera and does follow my movement. But since i unanchored the lens to use animations on it then it gives off this weird jitteriness to the camera (yes the lens part is welded to the camera). If i anchor the lens then the camera jitter resolves itself, but my character moves to the left ever so slightly.