Why can't I set ViewportFrame's CurrentCamera to a Camera?

Hey, there! Basically, I wanted an object to be in a GUI so to be able to do that I used a ViewportFrame, but then when I try to set the ViewportFrame’s current camera to a Camera which I created through a local script it would not set it properly. However, when I play test in studio and go to the ViewportFrame and set the CurrentCamera to the Camera manually it works but through a local script it would not work.

Here is the picture of how I set my ViewportFrame out:

image

andd here is the little snippet code that is in a local script:


local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ShopDialog = Player:WaitForChild("PlayerGui"):WaitForChild("ShopDialog")

local BlueSmoothie = game.ReplicatedStorage.Items.ViewportItems.BlueSmoothie:Clone()
BlueSmoothie.Parent = ShopDialog:WaitForChild("Frame"):WaitForChild("ViewportFrame")
BlueSmoothie:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
local Camera = Instance.new("Camera")
Camera.Name = "ViewportCamera"
Camera.Parent = ShopDialog:WaitForChild("Frame"):WaitForChild("ViewportFrame")
Camera.CFrame = CFrame.new(Camera.Parent.BlueSmoothie.Main.Position + Vector3.new(0, 10, 20), Camera.Parent.BlueSmoothie.Main.CFrame)

wait(5)
			
script.Parent.CurrentCamera = script.Parent:WaitForChild("ViewportCamera")

Turn on the output when working with scripts, and you will see that there’s a runtime error on line 11:

Camera.CFrame = CFrame.new(Camera.Parent.BlueSmoothie.Main.Position + Vector3.new(0, 10, 20), Camera.Parent.BlueSmoothie.Main.CFrame)

The second argument of the constructor you’re using has to be a Vector3 as well (use Main.CFrame.Position or simply Main.Position).

1 Like

I always turn on my output whenever I am working with scripts and also there isn’t any runtime error on the output. The viewport frame seems to work now when I use a Vector3 on the second argument of the CFrame.new() constructor so thanks for that, I marked your post as solution :slight_smile:

There’s no way that line doesn’t produce a runtime error when executed.

Interesting, because it really did not show anything, perhaps because I am using the new output which is in beta currently so thats why it is not showing. It’s best for me to revert my output back to the normal one until the new one gets out of beta. Try enabling the new output and it will not show the error, if it did for me then I would have easily solved it.