I have this script which changes the player’s avatar (character) for another one.
The thing is, when the change occurs, the GUI on game:GetService("Players").LocalPlayer.PlayerGui
gets deleted.
Note that I can’t clone it; when I do, the Viewport Frame’s Cameras as well as local scripts, for some reason, get deleted.
local Debris = game:GetService("Debris")
local Camera = game:GetService("Workspace").CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local NewCharacter
local Avatar
while true do wait()
for _, v in pairs (script.Parent:GetDescendants()) do
if v:IsA("TextButton") then
v.MouseButton1Up:Connect(function()
NewCharacter = v.Parent:FindFirstChildOfClass("Model"):Clone()
Avatar = Instance.new("StringValue")
Avatar.Name = "Avatar"
Avatar.Value = NewCharacter.Name
Avatar.Parent = Player.leaderstats
NewCharacter.Parent = game:GetService("Workspace")
NewCharacter.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame
NewCharacter.HumanoidRootPart.Anchored = false
NewCharacter.Name = Character.Name
Debris:AddItem(Character, 0.5)
Player.Character = NewCharacter
Camera.CameraSubject = NewCharacter
end)
end
end
Character:WaitForChild("Humanoid").Died:Connect(function()
if Avatar.Value ~= nil then
for _, v in pairs (script.Parent:GetDescendants()) do
if v.Name == Avatar.Value then
NewCharacter = v:Clone()
NewCharacter.Parent = game:GetService("Workspace")
NewCharacter.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame
NewCharacter.HumanoidRootPart.Anchored = false
NewCharacter.Name = Character.Name
Debris:AddItem(Character, 0.5)
Player.Character = NewCharacter
Camera.CameraSubject = NewCharacter
end
end
end
end)
end
That was the main problem, although I still have one left; whenever the player changes character, it won’t do animations when walking, jumping, or idlying anymore.