How to listen to when a character script is being destroyed inside that script

For a lot of my scripts in StarterCharacterScripts, I have the event humanoid.Died:Once() which performs a cleanup of the script. However, when I call LoadCharacter(), this doesn’t fire, so I need another way to clean up the scripts.

I’ve tried doing script.Destroying, player.CharacterRemoving, character.Destroying, and character.AncestryChanged, but none of these fire. Before calling LoadCharacter() , I also set the humanoid health to 0 and kept the humanoid.Died event in the starter character scripts, but that didn’t work.

Does anyone know what event I could use or what I can do to clean up the starter character scripts when the player dies/character is respawned?

This sounds like a stupid question, but I can’t find a way to do this.

Do player.CharacterAdded:Connect(–yourfunciton–) --this will run whenever you call loadcharacter()

I tested it on a baseplate and it doesn’t seem to work.

oh wait i forgot it is a startercharacter script so it wil not fire. I am confused as to what you are trying to do. Any code you put in the startercharater script will run as soon as loadcharater() is called

What are you trying to do? Could you give us a sample of your code or a video. :DD

YOu don’t need to clean up startercharater scripts. They are deleted when the charater dies.

I’m trying to run a function inside a script that is a child of the character when player:LoadCharacter() is called from another script.

image

In this script, I want to run a function like this,

game.Players.LocalPlayer.CharacterRemoving:Once(function()
	print("loadcharacter is called")
end)

but it doesn’t run.

I created OOP objects which need to be cleaned up by calling its :Destroy() function.

You cant do this since startercharatcerscripts are destroyed along with the character and created along with the character along with any connections/objects within those scripts.

Okay I guess the alternative (not the best) is the kill the player and let it respawn.