Morph killing player several times?

I’ve made a system where when a player joins in, they become their selected skin. For the skin transformation I’m using a simple morph script:

local morph = ReplicatedStorage.Characters:FindFirstChild(character):Clone()
morph.Name = player.Name
morph.HumanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame
player.Character = morph
morph.Parent = workspace

However, I’ve realised when using a :CharacterAdded event, the event exceeds the maximum event re-entry depth. My morph is killing the player several times, does anyone know a better way to make this morph or am I doing something incorrectly?

1 Like

If you have something like

player.CharacterAdded:Connect(function(character)
    player.Character = morph
end)

Then it would infinitely fire the CharacterAdded event because you’re adding a new character to the player inside of the CharacterAdded event

To fix this you could just wait for the character, instead of making a CharacterAdded event like this:

local Character = Player.Character or Player.CharacterAdded:Wait()
if Character then
	Player.Character = morph
end
1 Like

I didn’t even think about that, that you so much!

If your character can respawn, then you should probably do something like this:

local Debounce = false
local function OnPlayerAdded(Player)
	Player.CharacterAdded:Connect(function(Character)
		if not Debounce then
			Debounce = true
			Player.Character = morph
			Debounce = false
		end
	end)
end