Custom character glitched CharacterAdded?

Hello, I have an error in my code… The error is Maximum event re-entrancy depth exceeded for Player.CharacterAdded and because, in the script I need help with, the players character is changed, which makes the CharacterAdded function to occur again. I want the player to spawn in with a custom rig whenever they spawn in, which is how I have it right now. I could add it into StarterCharacter, but im goign to have multiple in the future and have different characters for different classes, so I am just testing out like this for now. Is there anyway how i can change the players character without having it break with the error above? And also, when I duplicate the “Animate” script, the animations in the character do not work, and the body colors dont change ;(

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local description = character.Humanoid:GetAppliedDescription()
		
		description.Shirt = "http://www.roblox.com/asset/?id=7828726929"
		description.Pants = "http://www.roblox.com/asset/?id=7828725684"
		

		
		local newChar = script.GOfficer:Clone()
		

		
		newChar.Parent = workspace
		

		
		
		newChar.Humanoid:ApplyDescription(description)
		
		player.Character = newChar
		for i,v in ipairs(character:GetChildren()) do
			if v:IsA("BaseScript") then
				v.Parent = newChar
			end
		end
		character:Destroy()
	end)
end)

1 Like

That error looks like it occurs when an event is caught in like an infinite kind of loop if that makes sense. My guess from looking is when you do player.Character = newChar it sets off the CharacterAdded event which goes back to setting the character which goes back to the loop and so on.

And is this what you’re trying to do currently? I don’t have any experience with custom rigs like you were saying so I can’t really help there but that’s all I really know to say.
https://developer.roblox.com/en-us/api-reference/function/Player/LoadCharacterWithHumanoidDescription

Yeah I figured this was the issue too. I dont think LoadCharacterWithHumanoidDescription would work, as the rig im using has custom parts that wont load with the regular character

Bump. Does anyone got any ideas for this?

bump part 2 Does anyone got any ideas?

Pretty sure asigning player.Character a new instance will fire the .CharacterAdded event creating an infinite loop

Add a task.wait(0.1) after the CharacterAdded function. This will ensure that it won’t break the system/game if the player dies infinetly.