Trying to make viewport frame show character in character create

Hey, I am new to working with viewport frames and am trying to make a character create for my game, and its come to my understanding that I must update the players model when they make changes to it (Skin, hair ect) but anyway for my updating script it won’t work.
Anyone know why this is not working on updating it? The body it refers to is a body that is already set up in front of the camera that it has, this is so I can teleport the players body to the dummy body’s location and then remove the dummy and replace it with the player.

Thank you for reading and all help is appreciated!
EDIT:
STILL not solved.
Current script

local Viewport = script.Parent
local player = game.Players.LocalPlayer
local Charact = player.Name
local Character = game.Workspace:WaitForChild(Charact)
script.Parent.body.HumanoidRootPart.CFrame = game.Workspace.Angles.CharArea.HumanoidRootPart.CFrame
local Cam = Instance.new("Camera")
Cam.Name = "Vyam"
Cam.Parent = game.Workspace
Cam.CFrame = CFrame.new(-84.51, 114.878, -271.133) *CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))
wait(2)
script.Parent.CurrentCamera = Cam
function update()
	local player1 = game.Players.LocalPlayer
local CharacterM = player1.Character:Clone()
local Current = script.Parent.body
Current.Parent = game.Workspace
CharacterM.HumanoidRootPart.CFrame = Current.HumanoidRootPart.CFrame
Current:Destroy()
CharacterM.Name = "body"
CharacterM.Parent = script.Parent
end

wait(3)
local Go = true

while Go do
	wait(1.5)
	update()
end

The Error I am getting is at line 17 it is saying CharacterM is a nil value.
Bumping this so new people see it still not solved.

1 Like

This problem is caused because you have not yet defined the function “update” when you call it in line 8. For example:

Script 1:
image
Output:
image

Script 2:
image
Output:
image

As you can see in Script 1, the function is defined after the script, which causes the error. However, in Script 2, the function is defined before the script, which allows it to be executed in line 4. Try this script for your game:

local Viewport = script.Parent
local player = game.Players.LocalPlayer
local Charact = player.Name
local Character = game.Workspace:WaitForChild(Charact)
function update()
     local CharacterM = player.Character:Clone()
     local Current = script.Parent.body
     CharacterM.HumanoidRootPart.CFrame = Current.HumanoidRootPart.CFrame
     Current:Destroy()
     CharacterM.Name = "body"
     CharacterM.Parent = script.Parent
end
for i,v in pairs(Character:GetChildren()) do
     v.Changed:Connect(function()
          if v:IsA("BasePart") then
               update()
          end
     end)
end

Also, if you put this in your game, you might want to replace the spaces with tabs. To do that, use the replace button (

) to replace 5 spaces with a tab.

2 Likes

Thank you, I don’t really use functions as much as I should so I guess I just did not realize that, thanks. I’ll test this out in a little bit if it works I’ll mark it solved.
EDIT: just tested it, seems it still won’t work.

If your character is invisible with no errors, it might just be that you haven’t set a camera yet.

1 Like

Well I looked into it and what I notice is when I go to where the camera should be located (its parent is the viewport frame) in game, its not there like it is in studio. Could this be why?

This is exactly why. I had this problem before, the camera disappears whenever you join the game. In order to get the camera in, you’ll just have to create a new one using Instance.new.

So I have to create a camera? Thanks. I’ll mark this as solved for now.

Yes, you’ll have to make your own camera and set the ViewportFrame’s camera to it.

Just tried this and for some reason its still not updating. Hold up im dumb, I forgot to move where I put the thing for the function. Alright just fixed it I forgot to set the current camera. Also, is it possible to change the angle the camera is because I think that’s why its not showing the character when I fixed the current camera just now.

Current script

local Viewport = script.Parent
local player = game.Players.LocalPlayer
local Charact = player.Name
local Character = game.Workspace:WaitForChild(Charact)
script.Parent.body.HumanoidRootPart.CFrame = game.Workspace.Angles.CharArea.HumanoidRootPart.CFrame
local Cam = Instance.new("Camera")
Cam.Name = "Vyam"
Cam.Parent = script.Parent.Parent
Cam.CFrame = CFrame.new(-85.834, 114.914, -271.394) *CFrame.Angles(math.rad(),math.rad(-90),math.rad())
wait(2)
script.Parent.CurrentCamera = script.Parent.Parent:WaitForChild("Vyam")
function update()
local CharacterM = player.Character:Clone()
local Current = script.Parent.body
CharacterM.HumanoidRootPart.CFrame = Current.HumanoidRootPart.CFrame
Current:Destroy()
CharacterM.Name = "body"
CharacterM.Parent = script.Parent
end
for i,v in pairs(Character:GetChildren()) do
v.Changed:Connect(function()
if v:IsA("BasePart") then
update()
end
end)
end

Testing some things out to see if they will fix it.

In your update() function, have you tried using ClearAllChildren() instead of referencing the body and destroying it, then parenting the new character after?

No I have not tried that but am going to rn.

Still receiving the same exact error at line 17.

Basically I have it set to destroy the empty model at the end. Did not know if there was a quicker way to move the children of characterm as well so I used a loop. Anyway maybe in the function I should add a wait for the character? Just attempted this. Now I did not get a error but its not updating the character model and instead just showing the place holder I have as a body

-- Other new code / current
local Viewport = script.Parent
local player = game.Players.LocalPlayer
local Charact = player.Name
local Character = game.Workspace:WaitForChild(Charact)
script.Parent.body.HumanoidRootPart.CFrame = game.Workspace.Angles.CharArea.HumanoidRootPart.CFrame
local Cam = Instance.new("Camera")
Cam.Name = "Vyam"
Cam.Parent = game.Workspace
Cam.CFrame = CFrame.new(-84.51, 114.878, -271.133) *CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))
wait(2)
script.Parent.CurrentCamera = Cam
function update()
	local player1 = game.Players.LocalPlayer
local Character = player1.CharacterAdded:Wait()
local CharacterM = Character:Clone()
local Current = script.Parent.body
Current.Parent = game.Workspace
CharacterM.HumanoidRootPart.CFrame = Current.HumanoidRootPart.CFrame
Current:ClearAllChildren()
for i,v in pairs(CharacterM:GetChildren()) do
	v.Parent = Current
end
CharacterM:Destroy()
end

wait(3)
local Go = true

while Go do
	wait(1.5)
	update()
end

I meant just calling ClearAllChildren() and then parenting the character model.

Like this:

function update()
      script.Parent.body:ClearAllChildren()
      game.Players.LocalPlayer.Character:Clone().Parent = script.Parent.body
end

Oh ok I’ll try that. I’ll let you know if that works.

FYI I made an edit to the post, forgot to include the .body part, my bad.

So should I clear the children at the start of the entire script? Because I need the humanoid root part so I can teleport the players character model to the right spot for the viewport frame.
Agh I take that back I can just store the CFrame of the humanoidrootpart of the body before deleting it, right?

Yea, you can either store the CFrame of the HRP and use that or you can CFrame the Camera so that it points in front of the character (or whereever you need to position it).

1 Like

Still not working but I just found somethin strange.

-- Current code

local Viewport = script.Parent
local player = game.Players.LocalPlayer
local Charact = player.Name
local Character = game.Workspace:WaitForChild(Charact)
local AreaS = script.Parent.body.HumanoidRootPart.CFrame
script.Parent.body.HumanoidRootPart.CFrame = game.Workspace.Angles.CharArea.HumanoidRootPart.CFrame
local Cam = Instance.new("Camera")
Cam.Name = "Vyam"
Cam.Parent = game.Workspace
Cam.CFrame = CFrame.new(-84.51, 114.878, -271.133) *CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))
wait(2)
script.Parent.CurrentCamera = Cam
function update()
	local player1 = game.Players.LocalPlayer
local Character = player1.CharacterAdded:Wait()
local CharacterM = Character:Clone()
local Current = script.Parent.body
CharacterM.HumanoidRootPart.CFrame = AreaS
Current:ClearAllChildren()
print("Cleared Children")
CharacterM.Parent = Current
print("Moved Character to Body")
end

wait(3)
local Go = true

while Go do
	wait(1.5)
	update()
	print("Updating..")
end

Notice how I got the prints I put in there? Well they aren’t sending any output what so ever.
Think I just found why they are not sending. I think the script is stuck on the wait at

local character = player1.CharacterAdded:Wait()

Just tested my theory and yep, its stuck at the wait for character added wait. I think this might be because 1. that may be only usable on the server. OR it may only respond when your character re spawns.

local Viewport = script.Parent
local player = game.Players.LocalPlayer
local Charact = player.Name
local Character = game.Workspace:WaitForChild(Charact)
local AreaS = script.Parent.body.HumanoidRootPart.CFrame
script.Parent.body.HumanoidRootPart.CFrame = game.Workspace.Angles.CharArea.HumanoidRootPart.CFrame
local Cam = Instance.new("Camera")
Cam.Name = "Vyam"
Cam.Parent = game.Workspace
Cam.CFrame = CFrame.new(-84.51, 114.878, -271.133) *CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))
wait(2)
script.Parent.CurrentCamera = Cam
function update()
	local player1 = game.Players.LocalPlayer
	print("Oh my")
local Character = game.Workspace:WaitForChild(player1.Name)
print("Character Found")
local CharacterM = Character:Clone()
local Current = script.Parent.body
CharacterM.HumanoidRootPart.CFrame = AreaS
Current:ClearAllChildren()
print("Cleared Children")
CharacterM.Parent = Current
print("Moved Character to Body")
end

wait(3)
local Go = true

while Go do
	wait(1.5)
	update()
	print("Updating..")
end

No errors except at line 20. Which is the line where I move the CFrame.