Try this
function FindReachPlayers( nPlayersCount )
if type( nPlayersCount ) == 'number' then
local nStart = 0
local pAddPlayer
local nIndexRemove = 0
local aReturnPlayers = { }
local aReachPlayers = { }
for _, pPlayer in pairs( getElementsByType 'player' ) do
table.insert( aReachPlayers, { [ pPlayer ] = getPlayerMoney( pPlayer ) } )
end
for n = 1, nPlayersCount do
for nIndex, a in ipairs( aReachPlayers ) do
for pPlayer, nMoney in pairs( a ) do
if nStart <= nMoney then
nStart = nMoney
nIndexRemove = nIndex
pAddPlayer = pPlayer
end
end
end
if not aReachPlayers[ nIndexRemove ] then
return aReturnPlayers
end
table.insert( aReturnPlayers, { [ pAddPlayer ] = nStart } )
table.remove( aReachPlayers, nIndexRemove )
nIndexRemove = 0
nStart = 0
end
return aReturnPlayers
end
return false
end
Updated again.
Example of use:
for _, a in ipairs( FindReachPlayers( 3 ) ) do
for pPlayer, nMoney in pairs( a ) do
outputChatBox( 'Player = ' .. getPlayerName( pPlayer ) .. ' , Money = ' .. nMoney )
end
end