How to detect when the player spawns

I would like to detect when the player spawns through a script. Is there a humanoid state that lets me detect this? Or some kind of event that I could use to detect when the player spawns? I would like to detect when the player spawns, when they join, and after they die / oof.

3 Likes

Here’s a list of relevant API references
https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded
https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died
e.g.

game.Players.PlayerAdded:Connect(function(player)
	print(player.Name.." joined")
	player.CharacterAdded:Connect(function(character)
		print(player.Name.." spawned")
		character.Humanoid.Died:Connect(function()
			print(player.Name.." died")
		end)
	end)
end)
17 Likes

Is it possible to do this on the client as well?

1 Like

The player on the client is retrieved via game.Players.LocalPlayer
https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer
PlayerAdded also works on the client but it will not fire for the players that are already in the game (because they were added before the player got there) or for the client itself (because they had to join first before their scripts run) but it’s typically not a good idea to connect to PlayerAdded from the client because there’s likely a cleaner way to handle it from the server.

CharacterAdded is fine; game.Players.LocalPlayer.CharacterAdded:Connect(...)
(so long as the script is in a place like StarterPlayerScripts where it isn’t removed and replaced every time the character is added)

2 Likes