You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to put the oldest player in the server on a specific team. An example of this system would be the GRG leader system.
What is the issue? Include screenshots / videos if possible!
I don’t know what function to use and the :GetPlayers() function only prints “nil”.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve looked on some other devforum posts but I haven’t found anything that works for my situation.
Here is the previous code I tried using.
local players = game:GetService("Players")
local oldestPlayer = players:GetPlayers()
print(oldestPlayer[1])
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
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
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
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:
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
Didn’t even know the function exsisted. Not to mention the fact that os.time() is based on your devices timezone might’ve caused his game system to break . Hopefully he sees this and fixes it