Help with Viewportframe

It now works
but it doesnt rotate the part/model like its supposed to

How is it supposed to rotate? What is it doing now?

It is not doing anything. In the code I want to be able to only show a specific model/part form the folder “Items” and have it rotate

local items = script.Parent:WaitForChild("Items")
local part = game.StarterGui.ScreenGui.shop.info.preview.Items.Anonymous_Desire
local vpf = script.Parent
part.Parent = items
items.Parent = vpf

local index, event = 1, nil

local camera = Instance.new("Camera")
camera.FieldOfView = 10
vpf.CurrentCamera = camera
vpf.Visible = true



-- rotation

local function setRotationEvent(model)
	local currentAngle = 0
	local modelCF, modelSize = model:GetBoundingBox()	
	
	local rotInv = (modelCF - modelCF.p):inverse()
	modelCF = modelCF * rotInv
	modelSize = rotInv * modelSize
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))
	
	local diagonal = 0
	local maxExtent = math.max(modelSize.x, modelSize.y, modelSize.z)
	local tan = math.tan(math.rad(camera.FieldOfView/2))
	
	if (maxExtent == modelSize.x) then
		diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
	elseif (maxExtent == modelSize.y) then
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
	else
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
	end
	
	local minDist = (maxExtent/2)/tan + diagonal

	return game:GetService("RunService").RenderStepped:Connect(function(dt)
		currentAngle = currentAngle + 1*dt*40
		camera.CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * CFrame.new(0, 0, minDist + 3)
	end)
end

local function addChildren()
    script.Parent.Items:WaitForChild("Anonymous_Desire")
    local part = game.StarterGui.ScreenGui.shop.info.preview.Items.Anonymous_Desire
	part.Parent.Parent = vfp
end

--

vpf.InputBegan:Connect(function(input)
	if (input.UserInputType == Enum.UserInputType.MouseButton1) then
	end
end)

but rn it is just showing the model and it is not rotating it

You have to call the setRotationEvent function and pass in the model as an argument, Also is there a reason you add the model to the viewportframe at the beginning of the script but you have a function later to do that . i also notice some unnecessary bits and some other problems so here is a simplified version of your code, where you need to define a folder that needs to be in the viewportframe (folder to hold currently visible model) and one that is outside of the viewportframe (that holds object that cannot be seen) it’s essentially what egomoose (where it appears this function is from) provided in his post.

local VisibleItems = script.Parent.VisibleFolder ---- the EMPTY folder within the viewportframe
local HiddenItems = script.Parent.Parent.HiddenFolder --- the items that you want to be placed in the viewport frame at some point (root path)
local PrevModel
local CurrentEvent


local vpf = script.Parent
local camera = Instance.new("Camera")
camera.FieldOfView = 10
vpf.CurrentCamera = camera
vpf.Visible = true


local function setRotationEvent(model)
	local currentAngle = 0
	local modelCF, modelSize = model:GetBoundingBox()	
	
	local rotInv = (modelCF - modelCF.p):inverse()
	modelCF = modelCF * rotInv
	modelSize = rotInv * modelSize
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))
	
	local diagonal = 0
	local maxExtent = math.max(modelSize.x, modelSize.y, modelSize.z)
	local tan = math.tan(math.rad(camera.FieldOfView/2))
	
	if (maxExtent == modelSize.x) then
		diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
	elseif (maxExtent == modelSize.y) then
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
	else
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
	end
	
	local minDist = (maxExtent/2)/tan + diagonal

	return game:GetService("RunService").RenderStepped:Connect(function(dt)
		currentAngle = currentAngle + 1*dt*40
		camera.CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * CFrame.new(0, 0, minDist + 3)
	end)
end


local function Rotate(Model)
	if CurrentEvent then
		CurrentEvent:Disconnect()
	end
	if PrevModel then
      PrevModel.Parent = HiddenItems
   end
   Model.Parent = VisibleItems	
   PrevModel = Model
   CurrentEvent =  setRotationEvent(Model)
end


Rotate(HiddenItems.ModelName) -- here is where you pass the hidden object so that it can be showen

And as an example of what i mean:
HierarchyExample


2 Likes

Yaaaaaaas TYSM!!! Finally something that works. A million thanks to you.

1 Like

ok one more problem.
I have a car model inside the hiddenfolder.
and now it doesnt show
it says this

Camaro is not a valid member of Folder
Players.no_clu360.PlayerGui.ScreenGui.shop.info_frame.info_1.preview.LocalScript’, Line 57

line 57 on the script is this

Rotate(HiddenItems.Camaro)

its basically the bottom of the script u help me on

You are getting that error because Camaro isn’t in the Hidden-items folder when you tried to reference it. if this is because not everything is finished loading/replicating then just add a wait for child Rotate(HiddenItems:WaitForChild("Camaro"))

It now says

Infinite yield possible on ‘Players.no_clu360.PlayerGui.ScreenGui.shop.HiddenFolder:WaitForChild(“Camaro”)’

Script ‘Players.no_clu360.PlayerGui.ScreenGui.shop.info_frame.info_1.preview.LocalScript’, Line 57

Then it must not be in the folder when you are trying to reference it, make sure the model’s name is exactly Camaro. When in-game is the model in HiddenFolder?

I found the solution
it just had to much folders inside of the model.
Thanks for your help anyways. :heart:

1 Like