ViewportFrame issue

A friend and I are creating a viewport frame . Every model has a primary part but it still doesn’t work.

            local viewportcamera = Instance.new("Camera",script)
            viewportcamera.CameraType = Enum.CameraType.Scriptable
            local yintager = .4
            local xintager = 2
            local r = 0
            local item = workspace[Item]
            local viewportpoint = Vector3.new(0,0,0)
            local viewportframe = Clone.ViewportFrame
            viewportframe.BackgroundTransparency = 1
            print(item)
            viewportframe.CurrentCamera = viewportcamera
            item:SetPrimaryPartCFrame(CFrame.new(viewportpoint))
            item.Parent = viewportframe
            game:GetService("RunService").RenderStepped:Connect(function()
                
                local cfrane, size = item:GetBoundingBox()
                local max = math.max(size.X,size.Y,size.Z)
                local distance = (max/math.tan(math.rad(viewportcamera.FieldOfView))) * xintager
                local currentdistance = (max/2) + distance
                viewportcamera.CFrame = CFrame.Angles(0,math.rad(r),0) * CFrame.new(viewportpoint + Vector3.new(0,(currentdistance*yintager),currentdistance),viewportpoint)
                r += 1
                
            end)

Error:

 Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.

The “item” model doesn’t appear to have it’s PrimaryPart property set. Thankfully, Roblox has recently introduced the “Pivot API”, so you can just replace the above line with

item:PivotTo(CFrame.new(viewportpoint))

And it should work just fine. What :PivotTo does is basically the same thing as :SetPrimaryPartCFrame, except it doesn’t need a primary part.

Thanks, it worked!

—Maximum character limit

Correct, PivotTo() uses the root part of a model instance instead, which is determined internally by the Roblox engine itself for all model instances (providing they contain at least a single BasePart instance).

1 Like

I believe the center is determined through something like :GetBoundingBox and the model’s pivot offset, not a part.