Solving once and for all. "Getting the Character after respawn"

Hello! I’ve wondered for a long time. Is this the only and best possible way to get and set a character variable? This would be a PlayerScript so it would also have to get the new character once died and re-loaded.

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

Player.CharacterAdded:Connect(function(newCharacter)
	Character = newCharacter
end)

1 Like

Seems like a good way to me, are you having any issues with it?

That’s how I’ve always done it.

Guessing you’re doing this via ModuleScript because of the CharacterAdded?

You could do something like this;

local Player = game.Players.LocalPlayer

local Character = Player.Character

local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()

Basically you are detecting if the humanoid died, and then you could add a few more lines of code to refresh what the Character variable is. In any way, if the character died, and the script is in their character or Gui it should automatically refresh. I would only suggest needing this if it is a PlayerScript.

No issues. I was just wondering if there was a shorter method to it.

Nooooo. Humanoid.Died is only applicable to the current character. CharacterAdded actually determines when a character respawns and thus a new character is available to be set as the variable. Died may subject you to more unneeded overhead or paths to set a new character.

I’ve always used this method except I’ve used CharacterAppearanceLoaded since that fires when all the necessary Instances inside the character have loaded.

Why do you need to store the character in a variable? You can just use the player.Character property to get the current character.

Because if you’re using character extensively saying “char” is easier than player.Character

Not really, and I never abbreviate things like that for the sake of readability. If you use scopes you can easily define character without risking it changing anyway.

Just personal preference, you can use comments when using variables or just simply name it character. All up to preference: :slight_smile:

I still prefer to use the most efficient and logical method, there are fewer things that can break and it is easier to read.