I need to know if I am able to get a players order on the playerlist for instance if a player was at the top of the list they would be player one. help would be appriciated.
1 Like
If you do game.Players:GetPlayers()[1] it will get the first player of the list.
local playerList = game.Players:GetPlayers() -- Returns a list with all the players
playerList[1] -- First person of the list and can be changed.
-- Let's say there are 3 players in the server in the moment this code is activated.
for i,v in pairs(game.Players:GetPlayers()) do
print(i,v) --[[
1 Player1
2 Player2
3 Player3
]]
end
1 Like
local allPlayerInServer = game.Players:GetPlayers() -- Getting all the players in the server
for i,v in pairs(allPlayerInServer) do
print("Player "..i.. " is "..v.Name --The names of the player in order
end
hope this helps
If you’re just trying to get the first player sorted without bias, use above code.
However, if you’re trying to get the top player by a stat bias (e.g. “Points”), you will have to use a script.
--!strict
--I use LuaU annotation to make code runa bit faster ;)
local players = game.Players:GetChildren() -- Get players
local top_player:number,top_player_score:number = 1,0 -- The index of a player, and the top score
for i:number,v:any in pairs(players) do -- For every player, do:
local stat:number = v.leaderstats.Points.Value -- Get score, must be a number
if stat > top_player_score then -- If they have a higher score than the current highest, then
top_player = i -- Get the player index from table, set it as top player
top_player_score = stat -- This is the top score
end
end
local top_player_instance:any = players[top_player] -- The top player, converted from a number to an actual player object.
print(top_player_instance.Name .. " has the top score in the server, with a score of " .. tostring(top_player_score))