CharacterAdded event runs too early

Hello!

I keep running into problems with the CharacterAdded event. It is used to find the new character after death but it is being executed too early causing some limbs to not be loaded tough causing an error.

I already tried adding wait() and using WaitForChild() ( “lag” ) whitout sucess.

Would there be a way to fix this or another way of finding the new character?

Thanks !

Yeah there is a way.

game:GetService("Players").PlayerAdded:Connect(function(plr)
    plr.CharacterAppearanceLoaded:Connect(function(character)
        -- character is your loaded character
    end)
end)

I did try doing this before but it didn’t work as this only executes when the player first logs in to the game and has his appearance loaded.

That’s strange because i have this function in my game and it adds a hitbox to character.
Alright i have just checked it and it works every time the character is added to the workspace. (not only when he enters the game)

Yeah CharacterAppearanceLoaded will only load once. You’ll want to use CharacterAdded.

Check it again with printing character’s name to be sure.

game:GetService("Players").PlayerAdded:Connect(function(plr)
    plr.CharacterAppearanceLoaded:Connect(function(character)
        print(plr.Name)
    end)
end)

i think he wants just edit some parts of the char only once

try running wait() right after character added event fires

so u could say:

game:GetService("Players").PlayerAdded:Connect(function(plr)
    plr.CharacterAppearanceLoaded:Wait()
       local char = plr.Character
end)

I’m trying to do a custom camera system which imply finding the head. This is the limb that I’m trying to get after the player dies so the script could work properly.

is on the client???

I’m using a local script to modify the camera. So, yes it is on the client.

Oh you should to tell it first.
Then you need local script not server.

Because you can change it’s camera only on a client.
Hm then you can just make a loop with preloadasync for all of the part of your character so.

so if u dont have the local script in the starter character scripts dont use the charcter appearence loaded event

I think you miss understoud, I am using a local script to control the camera.

Try this if you need to load it from client side.
Also you can make same from the server side.

  local ContentProvider = game:GetService("ContentProvider")
  ContentProvider:PreloadAsync(plr.Character:GetChildren())
  print(plr.Name)

ServerSide.

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		coroutine.wrap(function()
			local ContentProvider = game:GetService("ContentProvider")
			ContentProvider:PreloadAsync(Char:GetChildren())
			print(Player.Name.."'s Character loaded")
		end)()
	end)
end)

I don’t understand how I would go about using this. The function yields until all the given instances are loaded but in my case it is impossible to reference the head until it is loaded. This makes the method useless as the head would not be in the table making the function ignore it.

em you can just create an array and then add there your head
something like this

local array = Char:GetChildren()
table.insert(array,head)
ContentProvider:PreloadAsync(array)
print(Player.Name.."'s Character loaded")

Also wdym by

Do you have a custom head or something?
also i am intrested by this

Can you tell me why do you need to reference the head before it is loaded?

I have two possible solutions. One is to use task.wait() before trying to index the head. task.wait() makes sure a physics step runs before the thread continues. This step is where the limbs are added.

My second solution would to be add a
local head = character:WaitForChild("Head)
and index the head with that variable.

Examples:

Solution 1:

local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(character)
   task.wait()
   print(character.Head.Name)
end)

Solution 2:

local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(character)
   local head = character:WaitForChild("Head")
   print(head.Name)
end)

Hope this helps! Let me know if I understood the question wrong or you have any questions.

1 Like

My problem is that when the event fires, a lot of the times the head is not loaded in the character model making it impossible to reference it as it is not “there” ( loaded ). The reason I said

is because we want to use PreloadAsync() to check when it is loaded but it needs to know what to check which is already what we are trying to solve; we can’t use this as the head don’t “exist” until it is loaded.