"game.Players.LocalPlayer.CharacterAdded:Connect" Dont work

Hi there!
I do not understand why this script do not work.

game.Players.LocalPlayer.CharacterAdded:Connect(function()
    print("respawn")
end)

NOTE: I put this code in a local script and there are no print in output.

Could you help me please?

1 Like

Where is it located? If is probably being destroyed when the character dies, so it won’t be there to register when they respawn. For example if it was in starter character scripts, it would be destroyed when they die. Then when the character respawns the script would be recloned into their character, but by the time it was cloned it the character would already exist so the character added event wouldn’t respawn.

The parent of this script is the StarterGui instance.

Put it in StarterPlayerScripts, it gets replicated when you respawn since it is a parent of StarterGui

1 Like

Yes then it would be destroyed, unless the ScreenGui had ResetOnSpawn set to false. Put it in StarterPlayerScripts or put it in a ScreenGui with ResetOnSpawn set to false.

2 Likes

I’ve never seen the use of game.Players.LocalPlayer.CharacterAdded:Connect before and I don’t know if it actually serves a function. Maybe try game.Players.PlayerAdded:Connect instead.

Put it in StarterPlayerScripts as well.

If it is necessary to keep it in StarterGui, you may find the Player.Character page helpful:

LocalScripts that are cloned from StarterGui or StarterPack into a player’s Backpack or PlayerGui are often run before the old Character model is deleted. Player.Character still refers to a model, but that model’s parent is nil and it is has been destroyed. Because of this, if the Character already exists, you should check to make sure that the Character’s parent is not nil before using it.

The following code will run successfully when parented to StarterGui.

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

repeat wait() until character
print("respawn " .. character.Name) -- just to confirm that character is not nil

If you don’t need it in StarterGui, then I suggest doing what XdJackyBoiiXd21 recommended and simply move your current script over to StarterPlayerScripts.

I hope this helps!

1 Like

game.Players.PlayerAdded would fire when a player joins the game, not when their character respawns. game.Players.LocalPlayer.CharacterAdded is an event that allows you to know when a player’s character model has successfully loaded (but from a local script, of course).

1 Like

Gui most of the time is loaded after the character I believe, thus CharacterAdded will never be picked up by the script. Put it in a starterPlayerScript and reference the gui via player.PlayerGui[continue the path]

As other users have said, your :Connect call is running after the character is created. Instead, try something like this:

local function characterAdded(character)
    print("respawn")
end

local localPlayer = game.Players.LocalPlayer
do
    local character = localPlayer.Character
    if character then
        characterAdded(character)
    end
end
localPlayer.CharacterAdded:Connect(characterAdded)

You also might want to turn ScreenGui.ResetOnSpawn to false so the script doesn’t reset each time.

If you do decide to leave it on, you can just get the character’s player like this:
(all work, though the first method is unnecessary if ScreenGui.ResetOnSpawn is on)

local Workspace = game:GetService("Workspace")
local localPlayer = game.Players.LocalPlayer
local character = localPlayer.Character
while not character do
    Workspace.DescendantAdded:Wait()
    character = localPlayer.Character
end

--character added code:
print("respawn")

or

local localPlayer = game.Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

--character added code:
print("respawn")
1 Like