I need help fixing my viewportframe so it shows specific models in my items folder. In the script it only shows everything at once but I don’t wan that.
here is the code
local vpf = script.Parent
local items = script.Parent:WaitForChild("Items")
local children = items:GetChildren()
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 setIndexActive(newIndex)
if (event) then
event:Disconnect()
children[index].Parent = items
end
index = newIndex
event = setRotationEvent(children[index])
children[index].Parent = vpf
end
--
setIndexActive(index)
vpf.InputBegan:Connect(function(input)
if (input.UserInputType == Enum.UserInputType.MouseButton1) then
end
end)
any model/part that is a ‘child’ of the viewport frame will be viewed.
local function setIndexActive(newIndex)
if (event) then
event:Disconnect()
children[index].Parent = items
end
index = newIndex
event = setRotationEvent(children[index])
children[index].Parent = vpf
end
this function right here adds everything to your viewport frame, so if you don’t want to view everything at once then you’ll have to re-do this function and only add parts/models that you want to see.
local part = game.Workspace.SomePart
part.Parent = vfp
local function setIndexActive(newIndex)
if (event) then
event:Disconnect()
children[index].Parent = items
end
index = newIndex
event = setRotationEvent(children[index])
children[index].Parent = vpf
end
you would change this function into
local function addChildren()
--parent parts or models to the viewport frame here.
local somePart = game.Workspace.SomePart
somePart.Parent = vpf
end
so for the parent parts would I do something like this?
local function addChildren()
script.Parent.Items:WaitForChild("Whatever")
local part = game.StarterGui.ScreenGui.shop.info.preview
part.Parent = vfp
end
You are getting that error because you have a comma there with nothing behind it, also you might need /want to “silence” a W015 warning with a nil, so preferably in other words do:
[Players.no_clu360.PlayerGui.ScreenGui.shop.info.preview.LocalScript:10: attempt to index nil with ‘CurrentCamera’
Script ‘Players.no_clu360.PlayerGui.ScreenGui.shop.info.preview.LocalScript’, Line 10
local items = script.Parent:WaitForChild("Items")
local part = game.StarterGui.ScreenGui.shop.info.preview.Items.Anonymous_Desire
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
--
setIndexActive(index)
vpf.InputBegan:Connect(function(input)
if (input.UserInputType == Enum.UserInputType.MouseButton1) then
end
end)
it appears you don’t have vpf defined anywhere, try defining the path to your viewport frame somewhere in the first couple lines of your script (before it’s “used”)