Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I detect a player has spawned after a death rather than entering first time?

Often you would use a CharacterAdded event to fire something, but this would fire the event when the player comes in for the first time, how would I make it so that it only fires if the reason the player is respawning is because they died rather than entering the server?

4 comments

1

use remote functions or events and give them a parameter, which will mean wheather the player just joined or not

0

Had that idea but didn't think it'll work, but this just clarified thank you!

2

I wish i could down vote that comment.

1

i wish i could downvote ur face

2 answers

###Died

Humanoids have an event called Died. This event is fired when the player dies. You can use access the Humanoid by going through a player's character.

You can use this to perform actions when the certain players. For instance:


game.Players.PlayerAdded:Connect(function(plr) -- fires when a player joins
	plr.CharacterAdded:Connect(function(char) -- fires when the character loads
		char.Humanoid.Died:Connect(function() -- fires when the player dies
			print(plr.Name .. " has died!")
		end)
	end)
end)

###Wikia Links

Died Event

I'm assuming that you want to do this on the server.

You can use a table with Players as keys and booleans as values; when a Player first joins, assign their Player Instance to true. Whenever their character is added, check if the boolean is true. If so, assign it to false. If not, continue as normal to fire your event, execute your code, etc. We'll also remove the Player's field when they leave, to prevent a memory leak:

-- Services:
local players = game:GetService("Players")

-- Variables:
local player_first_chars = {}


-- Main:
players.PlayerAdded:Connect(function(player)
	player_first_chars[player] = true
	player.CharacterAdded:Connect(function(character)
		if player_first_chars[player] then
			player_first_chars[player] = false
		end
		-- ...
	end)
end)

players.PlayerRemoving:Connect(function(player)
	player_first_chars[player] = nil
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.