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.
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.
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
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.