Setting the Player.Character doesn't work

As the title says. It just completely bugs out and acts like I’m constantly dying
Looking for someone to help me with this.

game:GetService("Players").PlayerAdded:Connect(function(Player)	
	Player.CharacterAdded:Connect(function(Character)
		task.wait()

		local StarterCharacter

		if Player.Team == game:GetService("Teams")["Alien"] then
			StarterCharacter = game:GetService("ServerStorage"):FindFirstChild("AlienStarterCharacter"):Clone()

		else
			StarterCharacter = game:GetService("ServerStorage"):FindFirstChild("HumanStarterCharacter"):Clone()

		end

		StarterCharacter.Name = Player.Name
		Player.Character = StarterCharacter
		StarterCharacter.Parent = game:GetService("Workspace")

    end)
end)
1 Like
  1. Don’t use :FindFirstChild() unless there’s a chance of the instance not existing. Use :WaitForChild() instead.

  2. You need to preserve the original humanoid. Try setting the original character’s Humanoid’s parent to nil, replace the character, and parent the humanoid to the new character.

  3. Humanoids will basically try to kill themselves when you move them around, so right after you move the humanoid, use humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false).

1 Like

Sorry, I’m being dumb right now, how do I change the humanoid?

Sorry, I meant Parent. Change the original Humanoid’s Parent to nil.

Still nothing, unless I did something wrong.

		local Humanoid = Character.Humanoid
		Humanoid.Parent = nil
		
 		StarterCharacter.Name = Player.Name
		Player.Character = StarterCharacter
		StarterCharacter.Parent = game:GetService("Workspace")
		
		Humanoid.Parent = StarterCharacter
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

Do humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false) after setting it to nil as well.

No siree. Still the same issue.

		local Humanoid = Character.Humanoid
		Humanoid.Parent = nil
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
		
 		StarterCharacter.Name = Player.Name
		Player.Character = StarterCharacter
		StarterCharacter.Parent = game:GetService("Workspace")
		
		Humanoid.Parent = StarterCharacter
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

Games that replace the standard Roblox avatar start by unchecking Player->CharacterAutoLoads in the Explorer pane of your place. This way, you won’t have CharacterAdded firing automatically, and you won’t have to deal with Roblox code fighting with your code over who is setting Player.Character and when. It does mean that you have to watch for characters removing, to make sure respawning works.

WaitForChild is only needed for things that are replicating from another context, being created or cloned into existence by another script or coroutine. By the time your server starts running scripts, everything in ServerStorage is immediately available to FindFirstChild, and everything you copy with :Clone() is done synchronously, so all the cloned model’s children exist before your next line of Lua gets executed. Using FindFirstChild lets you immediately check for it to return nil, and catch errors like trying to clone something and getting the name wrong, whereas WaitForChild would just hang for as long as you let it.

Also, it’s generally not a good idea to try to transplant Humanoid instances between character models. Just copy over any property values you need to preserve.

Late response, but I got rid of the CharacterAdded line, and I disabled the CharacterAutoLoad, but now it’s erroring attempt to index nil with 'Team'