Jump to content

How to get the player with the biggest wanted level


N98E

Recommended Posts

Well, all what you have to do is, to make a loop for all players, using getElementsByType( "player", root )

then, use the function getPlayerWantedLevel as the table index and store the looped players to the table.

I coded this small code for you, not tested but it should work fine:

[lua]

local mostWanted = { }

for _, player in ipairs ( getElementsByType ( "player", root ) ) do
    mostWanted[getPlayerWantedLevel ( player )] = getPlayerName ( player )
end  

table.sort ( mostWanted, 
    function ( a, b ) 
        return a > b 
    end 
)[/lua]

 

 

Edited by KariiiM
Link to comment

I improved my code alitle bit, try it and tell me the results:

function getMostWantedPlayer ( )
    local mostWanted = { }
    local playerWL = { }
    for _, player in ipairs ( getElementsByType ( "player", root ) ) do
        mostWanted[getPlayerWantedLevel ( player )] = getPlayerName ( player )
        table.insert ( playerWL, getPlayerWantedLevel ( player ) ) 
    end  
    table.sort ( playerWL, 
        function ( a, b ) 
            return ( a > b )
        end 
    )
    return mostWanted, playerWL
end
  
addCommandHandler ( "gewl",
    function ( thePlayer )
        local mostWanted, playerWL = getMostWantedPlayer ( )
        outputChatBox ( "The most online wanted player is ".. tostring ( mostWanted[playerWL[1]] ) .."!", thePlayer, 255, 255, 0 )
    end 
)

 

Link to comment
22 hours ago, KariiiM said:

I improved my code alitle bit, try it and tell me the results:


function getMostWantedPlayer ( )    local mostWanted = { }    local playerWL = { }    for _, player in ipairs ( getElementsByType ( "player", root ) ) do        mostWanted[getPlayerWantedLevel ( player )] = getPlayerName ( player )
        table.insert ( playerWL, getPlayerWantedLevel ( player ) ) 
    end  
    table.sort ( playerWL, 
        function ( a, b ) 
            return ( a > b )
        end 
    )
    return mostWanted, playerWL
end
  
addCommandHandler ( "gewl",
    function ( thePlayer )
        local mostWanted, playerWL = getMostWantedPlayer ( )
        outputChatBox ( "The most online wanted player is ".. tostring ( mostWanted[playerWL[1]] ) .."!", thePlayer, 255, 255, 0 )
    end 
)

 

Here's one that doesn't need tables:

 

function getMostWantedPlayer ( )
    local mostWantedPlayer = false
    local mostWantedLevel = -1
    
    for _, player in ipairs ( getElementsByType ( "player", root ) ) do
        if getPlayerWantedLevel(player) > mostWantedLevel then
        	mostWantedPlayer = player
      		mostWantedLevel = getPlayerWantedLevel(player)
    	end
    end

    return mostWantedPlayer
end
  
addCommandHandler ( "gewl",
    function ( thePlayer )
        local mostWanted = getMostWantedPlayer ( )
    	if not mostWanted then
      		outputChatBox("There are no players on the server")
      		return
        end
        outputChatBox ( "The most online wanted player is ".. getPlayerName(mostWanted) .."!", thePlayer, 255, 255, 0 )
    end 
)

 

Edited by qaisjp
Link to comment
16 minutes ago, qaisjp said:

Here's one that doesn't need tables:

 


function getMostWantedPlayer ( )
    local mostWantedPlayer = false
    local mostWantedLevel = -1
    
    for _, player in ipairs ( getElementsByType ( "player", root ) ) do
        if getPlayerWantedLevel(player) > mostWantedLevel then
        	mostWantedPlayer = player
      		mostWantedLevel = getPlayerWantedLevel(player)
    	end
    end

    return mostWantedPlayer
end
  
addCommandHandler ( "gewl",
    function ( thePlayer )
        local mostWanted = getMostWantedPlayer ( )
    	if not mostWanted then
      		outputChatBox("There are no players on the server")
      		return
        end
        outputChatBox ( "The most online wanted player is ".. getPlayerName(mostWanted) .."!", thePlayer, 255, 255, 0 )
    end 
)

 

Using varialbe instead of table, will receive only one player, with table you can get more players in order, as I did for you, anyway you have the choice to choose what you need.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...