CharacterAdded fires multiple times when setting player.Character

Alright so im currently making a system that uses a custom character, Im setting the player.Character when the CharacterAdded Event fires. The issue is, by setting the player’s character, it triggers CharacterAdded causeing a infinate-like loop.

My current solution is a bit hackey and its to disable the connection(CharacterAdded) for a few seconds while setting the player.Character to the new model. But im wondering is there a better way to do this? Is there an event that I use instead of CharacterAdded for this type of system?

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		print("Character Added")

		local clone = character:Clone()
		clone.Name = player.Name
		clone.Humanoid.DisplayName = player.DisplayName
		clone.Parent = workspace
		clone.HumanoidRootPart.CFrame = workspace.SpawnLocation.CFrame
		player.Character = clone

		clone:PivotTo(CFrame.new(0,5,0))

		animateScript.Parent = clone
	end)
	
end)

Name your character model to “StarterCharacter” exactly and put it inside “StarterPlayer” folder.

1 Like

I’m going to add hacky to hacky and call it working.. ( I hope )

-- ServerScriptService/CharacterHandler

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)task.wait(1)
		if char:GetAttribute("oo") then return end

		local clone = character:Clone()
		clone:SetAttribute("oo", true)

		clone.Name = player.Name
		clone.Humanoid.DisplayName = player.DisplayName
		clone.Parent = workspace
		clone.HumanoidRootPart.CFrame = CFrame.new(0, 5, 0)
		animateScript.Parent = clone
		player.Character = clone
	end)
end)

It’s just a simple debounce..

1 Like

You are changing the character inside of .CharacterAdded which causes it to re-trigger. You should change the character outside of .CharacterAdded, and adapt the logic for CharacterAdded to only react if it is the character that you wanted it to be.

If you always want your character to be a specific thing then

My advice would be to disable automatic character creation, so you can control when a character should be created, which allows you to do a lot. https://create.roblox.com/docs/reference/engine/classes/StarterPlayer#LoadCharacterAppearance

2 Likes

Yes you could replace :Connect() with :Once() and put the model in StarterPlayer. Every time they will reset their character, it will put it back automatically.

Change :Connect to :Once, will make it fire only once.

1 Like

Why not just uncheck Players.CharacterAutoLoads if you’re giving everyone a custom character from storage. There’s no need to let their default avatar spawn if you’re not going to use it. There is a small amount of boilerplate code to enable respawn on death, but it’s given to you right on the CharacterAutoLoads API docs page: https://create.roblox.com/docs/reference/engine/classes/Players#CharacterAutoLoads