GUI gets deleted after changing player's character

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.

Just for understanding, you are trying to make a morph gui?

go to your ScreenGui and make sure ResetOnSpawn is off

1 Like

It was set to on.

I set it to off and now whenever I change characters Studio crashes.

yes sorry i didn’t read the whole script but the reson behind the crash it’s you making alot of connection

remove the while loop to fix the issue

Tysm! That worked rather perfectly :3

Btw, do you have a clue about why does this happen?

ye there’s localscript insiad your character called Animate make sure to clone it from the original character and place it into the new one

Health and Animate scripts are already there but it still isn’t working.

@Ahdlibya Nevermind, now that works fine. The only issue is that the character isn’t respawning after HP goes down to 0

1 Like