Why can't I switch the player's character?

Hi devs,
I have recently been trying to change the players character in game.
For some reason, the character just gets deleted, and doesn’t give the player a new character. I added a print statement (not included) to check that the function was being called when needed, which it was. Here’s my code
‘’
local function avatarCharLoad(plr)
local oldCharacter = plr.Character
local newCharacter = game:GetService(“ServerStorage”):WaitForChild(“Players”):FindFirstChild(plr.Name):Clone()

newCharacter.HumanoidRootPart.Anchored = false
newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)


plr.Character = newCharacter
newCharacter.Parent = game.Workspace.InGameChars.Protector
return plr

end
‘’
Anybody know what’s wrong?

(Soz that the code formatting didn’t work btw)

dont do it ever again please.
workspace global uses GETIMPORT so its far better.

local Protector = workspace.InGameChars.Protector

newCharacter:PivotTo(oldCharacter.PrimaryPart.CFrame)
newCharacter.HumanoidRootPart.Anchored = false
newCharacter.Parent = Protector
plr.Character = newCharacter
oldCharacter:Destroy()

return plr
1 Like

Dug up an old morph handler script I used to use that still works. Here’s what I used:

local charClone = morph:Clone()

local rootPart = charClone:FindFirstChild("HumanoidRootPart") 
local plrRoot = plr.Character:FindFirstChild("HumanoidRootPart") 
plr.Character = charClone
charClone.Parent = workspace
		
if rootPart and plrRoot then
	rootPart.CFrame = plrRoot.CFrame
end

Adjusting this to your needs:

local function avatarCharLoad(plr)
	local morph = game:GetService("ServerStorage"):WaitForChild("Players"):FindFirstChild(plr.Name)
	local charClone = morph:Clone()
	local rootPart = charClone:FindFirstChild("HumanoidRootPart") 
	local plrRoot = plr.Character:FindFirstChild("HumanoidRootPart") 
	plr.Character = charClone
	charClone.Parent = workspace
		
	if rootPart and plrRoot then
		rootPart.Anchored = false
		rootPart.CFrame = plrRoot.CFrame
	end
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.