Hello fellow developers! Sorry for creating a topic just for this but I can’t find much information on this. From the table game.Players:GetPlayers()
, will the player with the last index be the one that joined last? Thank you for responding (if you know the answer).
You should not rely on the order of the players returned by GetPlayers() to have any meaning. Even if you were to find that it happens to be ordered by join time right now, coincidentally, the API doesn’t guarantee this, so it would be unsafe to code with this assumption.
What you should do is connect an event listener to Players.PlayerAdded and manually store join times (or maintain your own sorted list). This is reliable.
local JoinedPlayers={}
function Sort()
local buffer = {}
for x=1,#JoinedPlayers do
local Lowest = math.huge
for i,v in pairs(JoinedPlayers) do
if v[2] < Lowest[2] then
Lowest = v
end
end
buffer[x] = Lowest
end
JoinedPlayers = buffer
end
game.Players.PlayerAdded:Connect(function(player)
table.insert(JoinedPlayers,{player, os.time()})
Sort()
end)
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(JoinedPlayers) do
if v[1] == player then
table.remove(JoinedPlayers, i)
Sort()
end
end
end)
1 Like