CharacterAdded not firing from StarterPlayerScripts

I have this script:

player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(Character)
	print(Character.Name)
	Character.Humanoid.Died:Connect(function()
		workspace.CurrentCamera.CameraSubject = player.Character.Head
	end)
end)

It’s supposed to set the players camerasubject to the head after death.
It works perfectly fine in the studio but whenever I try to play test the game on the website it doesn’t work.
I assume it’s probably because the character loads before the player (I also have a startercharacter so that may be breaking something too). It doesn’t print anything since it just doesn’t fire. Does anyone know how to fix this?

Think you got it spot-on.

Try:

local players = game:GetService("Players")

local player = players.LocalPlayer
local initChar = player.Character or player.CharacterAdded:Wait()

local function characterAdded(char)
    local function died()
        workspace.CurrentCamera.CameraSubject = char:WaitForChild("Head")
    end
    
    print(char.Name)
    char:WaitForChild("Humanoid").Died:Once(died)
end
task.defer(characterAdded, initChar)

player.CharacterAdded:Connect(characterAdded)

Found the solution. putting a localscript and checking whether the character has been loaded in it is unreliable. Just move the script to replicatedfirst and it will load before the character

Another way of doing this could be to just putting the script in StarterCharacterScripts and writing this:

script.Parent:WaitForChild("Humanoid").Died:Once(function()
   workspace.CurrentCamera.CameraSubject = script.Parent:WaitForChild("Head")
end)

I don’t think localscripts fire inside the character so you couldn’t do anything to the camera from a serverscript
Correct me if I’m wrong though

Well, localscripts do run inside the character.
Serverscripts, in fact, also run inside the character.
However, serverscripts only run when Workspace.RejectCharacterDeletions is set to Enabled.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.