Need help animating Viewportframes better

yooo, could someone help me on how to make my viewportframe better because this is my code right now (I wan’t to animate it)

local viewPortFrame = script.Parent:FindFirstChild("ViewportFrame")
local char = game.Workspace.ViewPortChars[script.Parent.Name]
 char.Archivable = true

 for i, ModelOrCam in pairs(viewPortFrame:GetChildren()) do
     if ModelOrCam:IsA("Model") or ModelOrCam:IsA("Camera") then
         ModelOrCam:Destroy()
     end
 end

 local newCamera = Instance.new("Camera")
 newCamera.Parent = viewPortFrame
 viewPortFrame.CurrentCamera = newCamera

 local clonedChar
 game:GetService("RunService").RenderStepped:Connect(function()
     if clonedChar then clonedChar:Destroy() end
     clonedChar = char:Clone()

     local hrp = clonedChar:WaitForChild("HumanoidRootPart")

     newCamera.CFrame = CFrame.new(hrp.Position + (hrp.CFrame.LookVector * 2.5) + Vector3.new(0,1,0) + (hrp.CFrame.RightVector * 0), hrp.Position) 
 
     clonedChar.Parent = viewPortFrame
end)

but because I cloning it all the time and spawning it in again its lagging way to much but idk how to make it better. could anyone help me?

You just answered your own question. Just don’t have it clone and delete every single frame? Why not just clone it once and update the CFrame each frame?

1 Like

If you try something like this it will only clone it if it doesn’t already exist

you could also change clonedChar:WaitForChild(“HumanoidRootPart”) to char:WaitForChild(“HumanoidRootPart”) if you needed to update the characters base cframe in the viewport for some reason – right now it doesn’t look like you need that from your cam positioning though

clonedChar = clonedChar or char:Clone()  -- if already exist it just sets else it will clone a new one

local hrp = clonedChar:WaitForChild("HumanoidRootPart") 

newCamera.CFrame = CFrame.new(hrp.Position + (hrp.CFrame.LookVector * 2.5) + Vector3.new(0,1,0) + (hrp.CFrame.RightVector * 0), hrp.Position) 

clonedChar.Parent = viewPortFrame
1 Like