Understanding Viewport frames

What do I want to achieve?

I’m trying to understand everything I can about how to use viewport frames and I’m trying to implement them into my simulator. I’m using them in my simulator by using 2 viewport frames in my shop to show items.

Issue?

I used this video to understand viewport frames which later in the video uses and instances a camera that shows the item I want to show in my viewport frame. The problem is, is that this method doesn’t work and there isn’t anything in the output to support me on this issue.

What did I try?

I tried looking at various videos on viewport frames, debugging my code, and making it so that the 2 potential parts in my VPF were not cloned.

Local script in Frame:

local rs = game:GetService("ReplicatedStorage")

local folder = rs:WaitForChild("FoodItems")
local cooked = folder.CookedBeef
local super = folder.SuperBeef
print(super)
print(cooked)

local frame = script.Parent
local VPF1 = frame.ViewportFrame1
local VPF2 = frame.ViewportFrame2

VPF1:ClearAllChildren()
VPF2:ClearAllChildren()

cooked.Parent = VPF1
super.Parent = VPF2

local newcam1 = Instance.new("Camera", VPF1)
local newcam2 = Instance.new("Camera", VPF2)

VPF1.CurrentCamera = newcam1
VPF2.CurrentCamera = newcam2

newcam1.CFrame = cooked.CFrame * CFrame.new(0,cooked.Size.Y * 1.5,0)
newcam2.CFrame = super.CFrame * CFrame.new(0,super.Size.Y * 1.5,0)
print("Full Success")

Elaboration on goal: The goal of this script is to automatically and always make the viewport frames show these cloned parts.

1 Like

It most likely is working, but your positioning is wrong. You should be using CFrame.lookAt. I’m also not sure how you want to position your camera, but currently, you set it up so that the camera is directly above the object.

You might also want to clone the items from ReplicatedStorage, so you can use them again.

Code:

local rs = game:GetService("ReplicatedStorage")

local folder = rs:WaitForChild("FoodItems")
local cooked = folder.CookedBeef
local super = folder.SuperBeef
print(super)
print(cooked)

local frame = script.Parent
local VPF1 = frame.ViewportFrame1
local VPF2 = frame.ViewportFrame2

VPF1:ClearAllChildren()
VPF2:ClearAllChildren()

cooked.Parent = VPF1
super.Parent = VPF2

local newcam1 = Instance.new("Camera", VPF1)
local newcam2 = Instance.new("Camera", VPF2)

VPF1.CurrentCamera = newcam1
VPF2.CurrentCamera = newcam2

newcam1.CFrame = CFrame.lookAt(cooked.Position + Vector3.new(0, cooked.Size.Y * 1.5, 0), cooked.Position)
newcam2.CFrame = CFrame.lookAt(super.Position + Vector3.new(0, super.Size.Y * 1.5, 0), super.Position)
print("Full Success")
1 Like

Oh yeah it works, it is the positioning, but how does “cooked.Position” and “super.Position” work in the Y-Coordinate of the new Vector3.

I’m just adding a new Vector3 to the original position which constructs a new position above the original position.

Pretty useful tutorial for CFrame.lookAt:

1 Like

Oh I see, I’ll look at that video, thanks!

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