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?
Humanoid
s 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)
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)