Changing player's character kills the player

Hi there! I’m currently trying to turn the player’s character into a custom one I made, though I am experiencing an issue. The player dies when I do it, then he goes back to normal (default roblox character).

This is the line of code causing the issue:

player.Character = customCharacter

I have tried searching, but can’t find any topics related to this that have a working solution.

Any help will be appreciated!

May you show the part of the code in controlling loading the morph?

Yea, here:

function morphPlayer(player)
	local model = game.ServerStorage.CustomMorph
	local morph = model:Clone()
	morph.Parent = workspace
	morph:MoveTo(player.Character.UpperTorso.Position)
	morph.Name = player.Name
	player.Character = morph
	return morph
end

Call this method whenever the player’s CharacterAdded event fires:

local PlayerService = game:GetService("Players") 

local function OnPlayerAdded(player)
   Player.CharacterAdded:Connect(function(character)
      morphPlayer(player) -- This method will be called whenever the player's character is added example: respawning
   end)
end

PlayerService.PlayerAdded:Connect(OnPlayerAdded)

Well you see, I tried this, but this results in the script recursively killing then re-spawning the player.

What happens is that

Player.CharacterAdded

gets called, which calls the morphplayer function. The morphplayer function then changes the player’s character, which causes the player to die.

Player.CharacterAdded

gets called again, and this repeats.

Do you have any ideas how to fix this? Thanks!

Can’t you name the custom player model “StarterCharacter” and put it in StarterPlayer?

No because I don’t want the player’s default character to be the custom morph. They have to manually choose it, and then they will turn into it.

I made a topic on this (and solved it) recently. Short version, Humanoid kills both characters when ownership changes. So you need to swap characters, then add Humanoid.

3 Likes

Thanks so much man! I spent way too long on that, but thanks to you, the issue is solved.

1 Like

Yeah, I feel that… I was staring at the character for hours…