Trouble cloning the character

I’m trying to clone the player’s character so I can then put it in a viewport frame, this is the script:

local plr = game.Players.LocalPlayer
local plrChar = plr.Character or plr.CharacterAdded:Wait()
local vpFrame = script.Parent

-----------------------

plrChar.Archivable = true

local char = plrChar:Clone()
char.Parent = vpFrame

for i, v in pairs(plrChar:GetChildren()) do
	
	local clonedPart = v:Clone()
	clonedPart.Name = v.Name
	
	clonedPart.Parent = char
	
end

plrChar.Archivable = false

local vpCam = Instance.new("Camera")
vpCam.CFrame = CFrame.new(char.HumanoidRootPart.Position + (char.HumanoidRootPart.CFrame.LookVector * 3), char.Head.Position)
vpCam.Parent = vpFrame

vpFrame.CurrentCamera = vpCam

But when I play the game, open Players, then go inside the viewport frame, there’s nothing inside the cloned character, no humanoid root part, no head, nothing (which makes the script not work, it says that humanoid root part isn’t a valid part of model)

2 Likes

Is the HumanoidRootPart isn't a valid member of Model" the problem or is it when you tried to clone it?

He does. See the original post’s code:

3 Likes

Sometimes the player doesn’t fully load in before firing CharacterAdded. Try waiting for CharacterAppearanceLoaded instead. There are plans to make the events more intuitive, but they’ve been put on hold:

2 Likes

So for now, is it advisable to rely on CharacterAppearanceLoaded instead of CharacterAdded until this update is live for all games? (This may post may be off-topic but I didn’t realized the Character isn’t parented yet to the DataModel once CharacterAdded fired)

1 Like

Often you don’t need to have the character initialized, in which case it’s more convenient to use CharacterAdded. I’d recommend using CharacterAppearanceLoaded though, yeah. Ideally I’d use a custom Bindable that fires when LoadCharacter returns, but that’d be a bit of a pain to code so I wouldn’t recommend that for every game.

(The current ordering is really unintuitive, I can understand the confusion.)

2 Likes

the problem is the stuff inside the character model isn’t cloned with the character, so I used In Pairs to clone everything inside the character, but even that didn’t work, when I open the viewport there’s the character, but there’s nothing inside it

I understand what u mean, but if you read my post carefully, this isn’t my problem, the problem is that what’s inside the character (the actual body parts), is not getting cloned and parented to the viewport frame, even though I’m using that In Pairs

Since what’s inside the character doesn’t get cloned my code does not work, since I use the HumanoidRootPart

try

for i, v in pairs(plrChar:GetDescendants()) do

im assuming you want everything else inside the character to clone as well.

that’s how GetDescendants works? Didn’t know that thank you!

But wait, when you clone something, aren’t the children cloned with it?

No it is not, you are just cloning the children of the character, not whats child to the child (lol)

If it helped, please mark my response as a solution

1 Like

When you clone something their children are cloned too, I already tested it before

Using GetDescendants doesn’t change anything, the character’s children still aren’t getting cloned

It was a stupid mistake, the character was being cloned before the parts inside it could load, I removed the In Pairs, added a WaitForChild and it worked fine, since cloning an item clones all its children too