How to refresh a Viewport frame?

  1. What do you want to achieve? Keep it simple and clear!
    I converted a ModuleScript into a LocalScript for a character customization GUI I’m trying to make; I only need the Viewport frame to refresh every so often to reflect any new changes to the character (i.e new hairstyles or skin color), without causing any performance issues.
  2. What is the issue? Include screenshots / videos if possible!

    It looks like the Viewport freezes right after the spawning of my character.
local TweenService = game:GetService('TweenService')
char = game.Players.LocalPlayer.Character


local function GenChar()
	if not char then return end
	char.Archivable = true
	local hum = char:WaitForChild('Humanoid')
	local hrp = char:WaitForChild('HumanoidRootPart')
	local c = char:Clone()
	char.Archivable = false
	for i,v in pairs(c:GetDescendants()) do
		if v:IsA('LocalScript') or v:IsA('Script') then
			v.Disabled = true
			v:Destroy()
		end
	end
	c.Parent = script
	c:WaitForChild('Humanoid').DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	c.PrimaryPart = c:WaitForChild('HumanoidRootPart')
	return c
end

player = game.Players.LocalPlayer
ViewportFrame = script.Parent.PlayerViewport

if not player or (player and not player:IsA('Player')) then warn('1st arguement missing; targetted player instance') return end
local viewport = ViewportFrame
local character = player.Character or player.CharacterAdded:Wait()
local camera = viewport:FindFirstChildOfClass('Camera') or Instance.new('Camera', viewport)
local char = GenChar(character)

char:SetPrimaryPartCFrame(CFrame.new(0,-2,5))
camera.CFrame = char.Head.CFrame * CFrame.new(1,.3,-5) 
camera.CFrame = CFrame.lookAt(camera.CFrame.Position, char.Head.Position)
camera.FieldOfView = 50
viewport.CurrentCamera = camera
char.Parent = viewport



local a = {
	char = char,
	camera = camera,
}

function a:TweenCameraCFrame(cframe, tweeninfo)
	local tween = TweenService:Create(camera, (tweeninfo or TweenInfo.new(1)), {CFrame = CFrame.lookAt(cframe.Position, char.Head.Position)})
	tween:Play()
	return tween
end

setmetatable(a, {__index = viewport})

return a

Have you tried using a WorldModel?

ViewportFrame should be refreshed every change of its child object. Your code seems to be little bit messy. So when you’re changing a hair of your character, make sure the character under ViewportFrame is changed. Every add/destroy or CFrame modification counts.

1 Like

yeah this is using a WorldModel

How should I go about refreshing it?