I’m trying to build an in-game store/avatar customizer.
I’d like to clone my player’s current character into a ViewportFrame.
However, cloning a player’s character seems to fail silently.
If I create a new doc in Studio, join and type this code into the command bar: local p = game.Players.Shedletsky.Character:Clone() p.Parent = game.Workspace
It throws because p is nil.
I don’t want to load a character from the website using CreateHumanoidModelFromUserId, because I am using custom characters. I want to clone my guy like I can do in Studio with a copy/paste.
I don’t understand why the character model is special.
I went the long way around and did this, but it breaks character animation and I have no idea why:
function charCopy(player)
local c = player.Character or player.CharacterAdded:Wait()
print("character loaded locally")
local k = c:GetChildren()
local result = Instance.new("Model")
for i = 1,#k do
k[i].Archivable = true
local t = k[i]:Clone()
t.Parent = result
end
result.Name = player.Name
return result
end
local char = charCopy(game.Players.LocalPlayer)
Models also have the archivable property, and character models have it set to false by default. Enabling the property (Character.Archivable = true) will allow the model to be cloned. This won’t change the situation with animations though, since animations are driven by a LocalScript. Check this post from boatbomber for a full solution to rendering a copy of your character locally in a ViewportFrame: Rendering the character with a ViewportFrame - #31 by boatbomber
This code works with respect to character animation. No idea why. Thanks for the tip about character models being Archivable = false.
function charCopy(player)
local c = player.Character or player.CharacterAdded:Wait()
print("character loaded locally")
c.Archivable = true
local result = c:Clone()
return result
end
My bad, animations work fine when the character is a descendant of playerGui. I was thinking of the issue that arises when you clone a player character to the workspace.