Hi, I want to make sure I have the best approach to a PlayerScript that gets the Character.
Not a CharacterScript, So the player.character acquisition will only run for the first time and I will have to connect to characterAdded to update the characterStored variable after that if I am correct.
If i remember correctly you could do something like this:
local Character = plr.Character or plr.CharacterAdded:Wait()
Can’t remember it fully however so if it is wrong i apologize.
that’s for CharacterScript. For PlayerScripts, which isn’t going to run again, that would cause a memory leak to a dormant model now in nil. So I just added characterAdded to override the variable and keep it updated to the relevant, in workspace character.
Ahh gotcha, Otherwise yeah the function to renew variables is the way to go about it
Depending on what you’re using this for, I’d reccomend getting the player using game.Players.LocalPlayer
and then connecting a function to either Player.CharacterAdded
or Player.CharacterAppearanceLoaded
, which will cause whatever function you connect to fire. The nice thing about these events is that they will give you the latest character with it.
Example of code for a client-sided script:
local player = game.Players.LocalPlayer
player.CharacterAppearanceLoaded:Connect(function(character)
--Do stuff with the character here.
end
So like
game.Players.LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
is as good as it’s going to get?
From what I know, that is the best way to get the most character whenever it is added, since it fires an event every time. You can also use Player.CharacterRemoving
to coordinate when the character is removed.