How do I improve this character ViewportFrame rendering code?

Hello, I made a script which renders your character in a ViewportFrame, I need some feedback. Here’s the code:

local RunService = game:GetService('RunService')
local Player = game.Players.LocalPlayer
local ViewPort = script.Parent.ViewportFrame
local Camera = Instance.new("Camera")
ViewPort.CurrentCamera = Camera

local tweenService = game:GetService("TweenService")

function RenderHumanoid(Character)
    local CharModel = Instance.new("Model", ViewPort)
    CharModel.Name = Character.Name;
    CharModel.PrimaryPart = Character.HumanoidRootPart
    local Model = Character:GetChildren();
    for i = 1, #Model do
        print()
        local child = Model[i]
        if not child:IsA("Script") and child.Name ~= "Belt" then
            child.Archivable = true;
            if child:IsA("Part") or child:IsA("MeshPart") then
                local renderClone = child:Clone()
                renderClone.Parent = CharModel
                renderClone.CFrame = child.CFrame
                game:GetService("RunService").RenderStepped:Connect(function()
                    renderClone.CFrame = child.CFrame
                    if CharModel.PrimaryPart then
                        tweenService:Create(Camera, TweenInfo.new(0.01, Enum.EasingStyle.Linear), {CFrame = CFrame.new(Character.HumanoidRootPart.Position + Character.HumanoidRootPart.CFrame.LookVector * 5, Character.HumanoidRootPart.Position)}):Play()
                    end
                end)
            else
                if child:IsA("Accessory") then
                    local accessory = child:Clone()
                    accessory.Parent = CharModel
                    game:GetService("RunService").RenderStepped:Connect(function()
                        accessory.Handle.CFrame = child.Handle.CFrame
                    end)
                else
                    child:Clone().Parent = CharModel
                end
            end
        end
    end
end

wait(2)

RenderHumanoid(Player.Character)

2 Likes