I couldn’t use player added event because this also gets called when players respawn after death. I need a way to run a code only when the player is added and this player happens to be the first player of the session as a whole.
In other words, inside the player added event handler, I would like to find out if this added player is the first one, not the respawns.
This is not correct. PlayerAdded only fires when the player object is added to the Players service. You most likely are using an other event or there’s a mistake in your code which leads to the behaviour that you’re seeing.
To find out whether it is the first player, you can do this:
local firstPlayerEncountered = false
game:GetService("Players").PlayerAdded:connect(function(player)
if firstPlayerEncountered then
return
end
firstPlayerEncountered = true
-- ( ... your logic here ... )
end)
Keep in mind that PlayerAdded doesn’t fire consistently in Play Solo before the first player is added, so you’ll have to do this instead if you want it to work in Play Solo:
local firstPlayerEncountered = false
local function playerAdded(player)
if firstPlayerEncountered then
return
end
firstPlayerEncountered = true
-- ( ... your logic here ... )
end
game:GetService("Players").PlayerAdded:connect(playerAdded)
for _,v in pairs(game:GetService("Players"):GetPlayers()) do
playerAdded(v)
end
You are right. Character gets added on each respawn but no the player. Can I think of this player being added in the Players service as one session? Basically if a player is removed from the Players service, that person has disconnected for whatever reasons.