Help using ViewportFrame to preview many items

Hello everyone,

I want to use ViewportFrames to preview items for a sort of dispenser machine, and I am familiar with how to set up one ViewportFrame, but I have two problems. One, there will be too many items to set up the ViewportFrames manually, and two, I don’t have the models for the items yet. Therefore, is there some way to make ViewportFrames display many items that are in a folder with a script?

To clear things up, I have a ViewportFrame inside of a Frame with a UIGridLayout and I need to duplicate the ViewportFrames to match the amount of items in a folder. This is easy enough, but I’m not too sure how to make every ViewportFrame display one of the items.

If anyone has any knowledge, any help is appreciated.

Thanks in advance!

1 Like

Use a for loop to loop through each item, and for each item, clone a template frame containing the viewport frame into the frame with the uigridlayout. Finally just clone the item into the viewport within the template frame for the respective item.

1 Like

This worked, thanks!

However, the previews are a bit small. Is there a way to make them larger?

1 Like

You can make it so that the camera view of the viewport is set a certain distance away from the primary part of the item/model

1 Like

So I ran into another problem - since I’m using tools for the items, I need the tool to be displayed in the ViewportFrame.

However, when I duplicate the tool’s Handle and parent it to the ViewportFrame, the ViewportFrames clone but no image is displayed.

Also, how do I move the camera closer to the target?

Here’s my script:

local mainFrame = script.Parent
local main = mainFrame.MainFrame

local vending = main.Vending
local temp = vending.Frame
local tbtn = script.Template

local tools = mainFrame.Tools

for i, tool in pairs(tools:GetChildren()) do
	if tool:IsA("Tool") then
		local new = tbtn:Clone()
		new.Parent = temp

		local viewportFrame = new.Item

		local part = tool.Handle:Clone()
		part.Parent = viewportFrame

		local viewportCamera = Instance.new("Camera")
		viewportFrame.CurrentCamera = viewportCamera
	end
end
1 Like

to move it closer, you can do something like viewportCamera.CFrame = part.CFrame * Vector3.new(0,5,0) for example. Essentially set the viewportcamera’s cframe to a cframe that is close to the part.

1 Like