CharacterAdded event not working in localscript

In my local script i can get the original player by using player.Character but when the player dies i want the character variable to change to the current character however my characteradded event is not firing at all

local lp = game:GetService("Players").LocalPlayer
local c = lp.Character


lp.CharacterAdded:Connect(function(char)
	print("char added")
	c = char
	if c:FindFirstChild("Humanoid") then
		print("hum")
		local hum = c:FindFirstChlid("Humanoid")
		if hum.Health <= 0 then
			c = nil
		end
	end
end)

This is a more simpler script I would use:


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded or player.CharacterAdded:Wait() -- waits for the character to be loaded
local Humanoid = character:FindFirstChild("Humanoid")

if Humanoid and Humanoid.Health == 0  then
    character = nil
end

1 Like

Try resetting your character and seeing what happens when you spawn back in

Where is this script located? If it’s in StarterCharacterScripts, then the character is gonna already be loaded in before the script is, so the .CharacterAdded event wouldn’t fire. I suggest putting this in StarterPlayerScripts

the problem is that when my player respawns i can use the player.character to get the first instance of the player but i want to get the character once he has died and then respawned.

Right, so that’s why you shouldn’t use StarterCharacterScripts. You won’t be able to run Player.CharacterAdded there (I don’t believe) since it’ll never detect the character being added. If you put it in StarterPlayerScripts, you should be able to have this detection.

1 Like
local lp = game:GetService("Players").LocalPlayer
local c = lp.Character or pl.CharacterAdded:Wait()

if c:FindFirstChildOfClass("Humanoid") and c:FindFirstChildOfClass("Humanoid").Health <= 0 then
       c = nil
end


lp.CharacterAdded:Connect(function(char)
	print("char added")
	c = char
	if c:FindFirstChild("Humanoid") then
		print("hum")
		local hum = c:FindFirstChild("Humanoid")
		if hum.Health <= 0 then
			c = nil
		end
	end
end)

sorry, writing on phone
also i would say you can use Humanoid.Died:Connect(function()