What Event Would I Use?

Hey there,

I need a function that runs when a player joins the game. CharacterAdded is only running after I die, but not when I first load in. Is there an event that can solve this?

Thanks for any and all help :grinning:

2 Likes

game.Players.PlayerAdded

This runs once the players joins (adds) to the game.

1 Like

If I die, will it run when I respawn too?

If you want to trigger a function when you die you’d use Humanoid.Died

It won’t run when you respawn.

1 Like

Okay, I’ll look into it. Thanks so much for replying!

1 Like

For this you’ll be using Players.PlayerAdded event, which will give you the player parameter being the Player instance object. Just like this:

game:GetService("Players").PlayerAdded:Connect(function(player)
    ... -- code
end)

Now, if you want something for this and do the “respawning” thing since this will only fire once the player joins, you’ll be doing such as this:

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        ... -- code
    end)
end)

And there you go! Basically CharacterAdded it is fired everytime the player is being removed and inserted into the workspace again.

1 Like

You don’t have to include :GetService, but it’s fine if you do. Just to shorten the code.

Consistency in code! It is better to have this type of practice, such as if you were doing :IsA("Class") instead of :IsA'Class' you’ll be using the () to keep the consistency and readibility of your code.

ok so when you write the function for Humanoid.Died, how do you do it? Because every single time I try it won’t catch it or fire.

Hey guys

The reason it wasn’t firing was because I needed to play it in the app, not in studio (it had to do with saving progress and all that). Thanks for helping me work through this to find the solution!