Run a function every time a player respawn

Hello, I’m currently stuck on a basic problem but I can’t seem to find the answer.
I want to run a function when a player spawns for the first time, and every time he respawns.

I think it got something to do with .CharacterAdded but I don’t see how.
Thank you.

2 Likes
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
   -- First time player joins
   player.CharacterAdded:Connect(function(char)
      -- when their character loads in
   end)
end)
13 Likes

Here is an example:

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(player) --the "player" variable will be defined automatically by the player added event
   player.CharacterAdded:Connect(function(character)  --the "character" variable will be defined automatically by the character added event
       --you can do whatever you want here, for example:
       --you can kill the player when he spawns
       local Humanoid = character:WaitForChild("Humanoid")
       Humanoid.Health = 0
   end)
end)
2 Likes

Adding to what @TheDCraft wrote, if you wish to skip the first time the player respawns so ignoring when the player join the game I suggest you put player.CharacterAdded:Wait() before the CharacterAdded connection like so:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
   -- First time player joins
   player.CharacterAdded:Wait()
   
   player.CharacterAdded:Connect(function(char)
      -- when their character loads in
   end)
end)
4 Likes