Laggy with using viewportFrames

so this is my code for using viewportFrames but its suppy laggy and idk why


	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 * 3) + Vector3.new(0,1,0) + (hrp.CFrame.RightVector * 0), hrp.Position) 
		--I fiddled around with the position of the camera so you get a little bit of a side on view.
		clonedChar.Parent = viewPortFrame
	end)

video: 2023-04-28 18-44-08

Its probably because you create a character every frame and delete it the next

but if I don’t they arent really animated

Play animations inside of the viewport frame. Don’t clone the player thousands of times to fake an animation.

and do you know how I could do that?

If you want to make sure the clone is always the exact same as as the character you can determine each parts CFrame relative to the HRP. With this information you may apply it to the clonedChar to get an exact replication of where it is.

can you give me a simple code doing what you said because I don’t really get what you mean sorry.

Something along these lines I think

local cloned
local hrpPos = sourceCharacter:FindFirstChild("HumanoidRootPart").Position
for _, bodyPart: Instance in pairs(sourceCharacter:GetChildren()) do
	if not bodyPart:IsA("BasePart") then continue end
	local clonedP = cloned:FindFirstChild(bodyPart.Name)
	local relCF = clonedP.CFrame - hrpPos
	clonedP.CFrame = relCF
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.