What does the code do and what are you not satisfied with?
It creates Viewport Frames that display an item’s model and the camera is positioned through premade constants based on what type of item it is, and it does the job, but the constants have to be set manually for every type of item
What potential improvements have you considered?
Possibly some sort of automation, but I can’t think of a method to do this that would display the item models perfectly
How (specifically) do you want to improve the code?
Either through automation, or some sort of improvement to what I’m currently doing that will make the constants easier to manually create
Viewport Constants Module
return {
Sword = {
frameFov = 2,
frameDistance = 125,
rotationOffset = CFrame.Angles(0, 0, 0),
frameCameraAngle = CFrame.Angles(0, 0, math.rad(45))
},
Armor = {
frameFov = 2,
frameDistance = 175,
rotationOffset = CFrame.Angles(0, math.rad(90), 0),
frameCameraAngle = CFrame.Angles(0, 0, 0)
},
Scythe = {
frameFov = 2,
frameDistance = 200,
rotationOffset = CFrame.Angles(0, math.rad(90), math.rad(90)),
frameCameraAngle = CFrame.Angles(0, 0, 0)
},
ItemList Module
-- SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- MODULES
local modules = ReplicatedStorage:WaitForChild("Modules")
-- VARIABLES
local itemModels = ReplicatedStorage:WaitForChild("ItemModels")
local ItemList = {
Sword_Of_Blue_Epicness = {
type = "Weapon",
modelViewportType = "Sword",
model = itemModels:WaitForChild("SwordOfBlueEpicness"),
},
Knight_Armor = {
type = "Armor",
modelViewportType = "Armor",
model = itemModels:WaitForChild("Knight Armor"),
},
Purple_Scythe = {
type = "Weapon",
modelViewportType = "Scythe",
model = itemModels:WaitForChild("Scythe"),
}
}
return ItemList
Function that handles the viewportframes
local function onItemUpdated(GUID, item)
local itemFrame = itemFrames[GUID]
if not itemFrame and item then
itemFrame = itemFrameTemplate:Clone()
itemFrame.Name = GUID
itemFrame.Parent = backPackScrollingFrame
itemFrames[GUID] = itemFrame
local viewPort = itemFrame.ViewportFrame
local itemInfo = ItemList[item.id]
local infoModel = itemInfo.model
local frameConstants = ViewportConstants[itemInfo.modelViewportType]
local frameFov = frameConstants.frameFov
local frameDistance = frameConstants.frameDistance
local rotationOffset = frameConstants.rotationOffset
local frameCameraAngle = frameConstants.frameCameraAngle
local model = infoModel:Clone()
model.Parent = viewPort
local modelCF = model:GetBoundingBox()
local modelPos = modelCF.Position
local offsetModelCF = modelCF * rotationOffset
local cameraCF = CFrame.new((offsetModelCF * CFrame.new(frameDistance, 0, 0).Position), modelPos) * frameCameraAngle
local camera = Instance.new("Camera")
camera.CFrame = cameraCF
camera.Focus = cameraCF
camera.FieldOfView = frameFov
camera.Parent = viewPort
viewPort.CurrentCamera = camera
updateUISizes()
elseif itemFrame and not item then
itemFrame:Destroy()
end
end
UI