I am attempting to find a player in my game who has the highest leaderstats value, known as 'eggsFound'
_G.eggHunters = {} for i,v in pairs(game.Players:GetPlayers()) do if v ~= nil then table.insert(_G.eggHunters, v.Name) end end for _, v in pairs(_G.eggHunters) do end
This is all I have right now, I'm unsure of what else I can try to find the person with the highest leaderstats value known as 'eggsFound', any help would be appreciated! Thanks!
You can do this using tables, table.insert()
and table.sort()
:
_G.eggHunters = {} for i,v in pairs(game.Players:GetPlayers()) do if v ~= nil then table.insert(_G.eggHunters, v) -- store the player instead of their name end end local function getHighestEggsFound() local TempTable = {} -- this table would be used to store each player's eggs found number for _, player in pairs(_G.eggHunters) do local leaderstats = player:FindFirstChild("leaderstats") local eggsFoundValue = (leaderstats and leaderstats:FindFirstChild("eggsFound") if eggsFoundValue then -- 'table.insert(x, y, z)' has 3 arguments. x = table(array), y = number position (optional), z = variable/object/instance your adding to the table table.insert( TempTable, { player, eggsFoundValue.Value } } end end if #TempTable >= 1 then --'table.sort(x, y)' has 2 arguments. x = table(array), y = function (optional) table.sort( TempTable, function(a, b) -- 'a' and 'b' is the data/table inserted into the TempTable -- we are comparing the distances and ordering them from greatest to least -- so we get the data's second value a[2] and b[2] which is the eggs found number and compare them return a[2] > b[2] end ) print( TempTable[1][1], -- player "has the most with", TempTable[1][2], -- eggs found number "eggs found!" ) end end