Put the oldest player in the server on a team

You are trying to get the oldest player, you mean the player that stayed the most in the server or the one with the highest age?

The one that has stayed in the server the longest.

Then I suggest you give every player a leaderstats point like time, and every second give them 1 point more, than every second check which player has the most points and put it in a team

I wrote this on devforum so please check that the code is write and fix all syntax errors b4 you say it doesn’t work :slight_smile:

local Players = game:GetService("Players")

local JoinTimes = {}

Players.PlayerAdded:Connect(Player)
      JoinTimes[Player] = time()
      Player.AncestryChanged:Connect(function()
              JoinTimes[Player] = nil
      end)
end

function GetOldestPlayer()
      local OldestTime = math.huge
      local OldestPlayer
      for Player, Time in JoinTimes do
            if Time <= OldestTime then
                  OldestTime = Time
                  OldestPlayer = Player
            end
      end
      return OldestPlayer

end

Thanks for the idea. characters…


I don’t seem to understand what is going on

so basically it makes a list called join times. This list literally stores the players join times:

JoinTimes = {
   --Player Name = Time Joined
   TwoMadDev = 2542,
    nikebrand1d = 43132
}

the “GetOldestPlayer” function loops through this list to find the player with the earliest join time.

Sorry I’m pretty bad at scripting. How do I print the results?

Just do:

print(GetOldestPlayer())

Ok I made the print statement but it doesn’t output anything

That’s because you have log mode enabled.

You have to do something like this, you have to modify the script

I made the leaderstat script that gives the player 1 point each second but I can’t find a way to grab the person with the highest amount of points.

Here’s the way

I tried a sorting system but it only prints nil.

local players = game.Players:GetPlayers()

table.sort(players, function(a,b)
	if a.leaderstats.Time.Value > b.leaderstats.Time.Value then
		return true
	end
end)

print(players[1].Name .. " has the most time!")

I would Recommend using time() as it returns the elapsed time since the Server Began, so you can assign this Number to the Player, Instead of having a number in the billions, it would be less, as os.time and tick return the elapsed time since the UNIX epoch (January 1, 1970), Plus, they are based on UTC Time, so it would probably be best to track their Join Time based on the Server’s Lifetime, Look into it if you like:

Basically using @TwoMadDev 's Example:

local Times = {}

game.Players.PlayerAdded:connect(function(p)
    Times[p.UserId] = time() -- elapsed time since Server Began
end)

game.Players.PlayerRemoving:connect(function(p)
    Times[p.UserId] = nil -- removes Player from list when leaving
end)

It is best to note that we are using UserId instead of Names, It is a lot more Efficient to Keep their UserId so we can keep track of them easier.

And to check the Oldest Player, we need to look for the shortest Value out of all the keys in the Table

function GetOldestInServer()
    local UserId
    local ShortestTime = math.huge -- infinite in simple terms 
    for index, Time in Times do
    -- index is the key (aka the UserId)
    -- Time is the Key's Value (aka their Time)
         if Time < ShortestTime then
            ShortestTime = Time -- new Value to look through, this makes it so it looks for a value lower than this one
            UserId = index -- This is so we can keep track of the Player we are literating through
        end
    end
    return tonumber(UserId), ShortestTime -- returns the UserId and Time of the Player
end
local PlayerId, PlayerTime = GetOldestInServer() -- Data is given to the two Variables
local Player = game.Players:GetPlayerByUserId(PlayerId) -- Gets Player in Server by their UserId
if Player then
    print(Player.Name, "Is the Oldest Player with the Time of", PlayerTime)
end
1 Like

Didn’t even know the function exsisted. Not to mention the fact that os.time() is based on your devices timezone :flushed: might’ve caused his game system to break :joy:. Hopefully he sees this and fixes it

1 Like

It even says it under the Documentation:

This library currently serves the purpose of providing information about the system time under the UTC format.

Ohhh ok. I’m starting to understand what @TwoMadDev said. I’ll look into the articles to try to understand them more. Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.