So I’ve been making this system that displays different character models in a viewportframe. One of those characters is the local player. When the player joins the game, an instance of a blank character template is cloned and parented to a file in replicatedstorage, then humanoid descriptions are used to make the character customized to the player. This code all works fine, but I’ll add it for reference-
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:Connect(function(char)
if not game.ReplicatedStorage.ClientResources.ViewPortItems.PlayerSpecific:FindFirstChild(plr.Name .. "Viewport") then
local Players = game:GetService("Players")
local customviewportmodel = game.ReplicatedStorage.ClientResources.ViewPortItems.NoneViewport:Clone()
customviewportmodel.Parent = game.ReplicatedStorage.ClientResources.ViewPortItems.PlayerSpecific
customviewportmodel.ItemPart.Humanoid:ApplyDescription(Players:GetHumanoidDescriptionFromUserId(plr.UserId))
customviewportmodel.Name = plr.Name .. "Viewport"
end
end)
end)
When I parent this instance back to workspace, everything seems to be in order, including a properly sized bounding box (which isn’t the case later).
So, the problems arise when the local player attempts to parent this instance to their viewportframe. There are no problems parenting other pre-made outfits to the viewport that are not cloned. These problems seem to be isolated solely to when I attempt to render cloned instances.
This is what a normal render looks like, for an outfit that is not cloned and modified:
For some reason, if the player is wearing certain accessories, the accessories appear to have moved far away and drastically increased the size of the bounding box, making it appear zoomed out.
When the character is not wearing these accessories, they have no body and no hair, but the bounding box is at least normal:
I am not sure what to do because they seem to clone just fine and I only experience issues when parenting them to the viewport. Here is the code that manages the viewport display:
local ViewportCamera = Instance.new("Camera", script)
ViewportCamera.CameraType = Enum.CameraType.Scriptable
local R = 135 -- start at 135 deg rotation
local Item = game.ReplicatedStorage.ClientResources.ViewPortItems[script.Parent.ViewportValue.Value .. "Viewport"] --Grab the viewport item to parent
local ViewportFrame = script.Parent:WaitForChild("Viewport")
Item:MoveTo(Vector3.new(0,0,0))
local ViewportPoint = Item.PrimaryPart.Position
ViewportFrame.CurrentCamera = ViewportCamera
Item:SetPrimaryPartCFrame(CFrame.new(ViewportPoint))
Item.Parent = ViewportFrame
local zoom = Item.ZoomScale.Value --How much the viewport should be zoomed in- manually set
local plr = game.Players.LocalPlayer
local function keepUpdated() -- Function that spins the camera around the object
local cframe, size = Item:GetBoundingBox()
local Max = math.max(size.X,size.Y,size.Z)
local distance = (Max/math.tan(math.rad(ViewportCamera.FieldOfView))) * zoom
local currentdistance = Max/2 + distance
ViewportCamera.CFrame = CFrame.Angles(0,math.rad(R),0) * CFrame.new(ViewportPoint + Vector3.new(0,currentdistance*.3,currentdistance),ViewportPoint)
R = R + 0.75 --increase rotation
end
game:GetService("RunService").RenderStepped:Connect(function()
keepUpdated()
end)
local viewportvalue = script.Parent.ViewportValue
viewportvalue.Changed:Connect(function() --Basically just updates all of the information that was initialized at the top
--This block handles differentiating the cloned character object vs default outfits
if viewportvalue.Value == 'None' then
Item.Parent = game.ReplicatedStorage.ClientResources.ViewPortItems
else
Item.Parent = game.ReplicatedStorage.ClientResources.ViewPortItems.PlayerSpecific
end
if viewportvalue.Value == 'None' then
Item = game.ReplicatedStorage.ClientResources.ViewPortItems.PlayerSpecific[plr.Name .. "Viewport"]
else
Item = game.ReplicatedStorage.ClientResources.ViewPortItems[script.Parent.ViewportValue.Value .. "Viewport"]
end
R = 135
ViewportPoint = Item.PrimaryPart.Position
Item:SetPrimaryPartCFrame(CFrame.new(ViewportPoint))
Item.Parent = ViewportFrame
zoom = Item.ZoomScale.Value
end)
I know this a lot, I’m just trying to paint the best picture I can to answer any possible future questions. To summarize: cloned object looks wonky in viewport, but normal in workspace. Thanks in advance!
Edit: I found a workaround that seems to work. My solution was to parent the cloned object to the workspace for a small period of time, for some reason this fixes the rendering issue.