Set Character Model After Spawning

I want to change the player’s character after spawning, so that I can utilize the stock Roblox spawning (timers, spawnpoints, etc… don’t want to do everything from scratch). It picks a random model from a set list based on what team the player chose.

I cannot use HumanoidDescription since some of the models have altered parts (still valid R6). StarterCharacter is not the functionality that I need.

I’ve got this so far and it partially works but I keep getting “Maximum event re-entrancy depth” even though I can’t see how the task is self referencing/sync dependent. The functionality is almost what I want except the PivotTo is not correct, presumably because the old character model hasn’t teleported yet? And also the very first character model does not Destroy, but all ones after that do.

local function ReplaceCharacter(newModel : Model, oldModel : Model)

		local newModel : Model = TeamModelPicker.GetRandomCharacterModel(ply.Team.Name)
		newModel.Name = ply.Name
		newModel.Parent = game.Workspace.Characters
		ply.Character = newModel
		task.defer(ReplaceCharacter, newModel, starterModel)


- Problem 1
( I don’t think you have to destroy the old player character because I think the game remove the old character automatically after you switch plr.Character to a new one.)

So, you should probably change the newCharacter location to currentCharacter location before switching plr.Character. And you don’t need to destroy the old character manually by code.

- Problem 2
Also, CharacterAdded event also fires when you set the player character to a new one. Which might be the source of your “Maximum event re-entrancy depth” error.

So, to fix this error, you can put the event inside a variable so you can disconnect it and add it back, before and after setting the new character respectively, to prevent the error, like this:

function onCharacterAdded(plr, char, characterAddedEvent)
	-- code...
	characterAddedEvent:Disconnect() -- remove character added event temporarily for preventing error when setting new character
	plr.Character = newCharacter -- SET YOUR NEW CHARACTER HERE
	characterAddedEvent = plr.CharacterAdded:Connect(function(char) -- add character added event back
		onCharacterAdded(plr, char, characterAddedEvent)
	-- code...

	local characterAddedEvent

	characterAddedEvent = plr.CharacterAdded:Connect(function(char)
		onCharacterAdded(plr, char, characterAddedEvent)
1 Like

Sorry, know it’s been over a week but I appreciate the reply.

We didn’t write exactly what you have but took roughly the same idea and everything works great now.

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