Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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?

1
use remote functions or events and give them a parameter, which will mean wheather the player just joined or not User#23252 26 — 5y
0
Had that idea but didn't think it'll work, but this just clarified thank you! Marmalados 193 — 5y
2
I wish i could down vote that comment. DinozCreates 1070 — 5y
1
i wish i could downvote ur face Gey4Jesus69 2705 — 5y

2 answers

Log in to vote
1
Answered by
green271 635 Moderation Voter
5 years ago

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

Ad
Log in to vote
1
Answered by
BenSBk 781 Moderation Voter
5 years ago
Edited 5 years ago

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)

Answer this question