Jump to content

[HELP] Top Damage


Doffy

Recommended Posts

function getTopThree()
    sortedTable = {}
    for player,damage in pairs(damageTable) do
        if isElement(player) then
            local team = getPlayerTeam(player)
                table.insert(sortedTable, {player, damage})
            end
        end
    table.sort(sortedTable, function(a,b) return a[2] > b[2] end)
    local t1, t2, t3 = {}, {}, {}
    t1 = {getPlayerName(sortedTable[1][1]), sortedTable[1][2]} or {"N/A", "0"}
    t2 = {getPlayerName(sortedTable[2][1]), sortedTable[2][2]} or {"N/A", "0"}
    t3 = {getPlayerName(sortedTable[3][1]), sortedTable[3][2]} or {"N/A", "0"}
    triggerClientEvent(root, "showTopZDamage", root, t1, t2, t3)
end

 

i have a problem on when i want to get t2 and t3 because there's no player 
 " attempt to index field '?' ( a nil value ) "

 

Link to comment

This error occurs because sortedTable[2][1] or sortedTable[3][1] returns nil when there are no players and you are attempting to call getPlayerName on a nil value
 

function getTopThree()
    sortedTable = {}
    for player, damage in pairs(damageTable) do
        if isElement(player) then
            local team = getPlayerTeam(player)
                table.insert(sortedTable, {player, damage})
            end
        end
    table.sort(sortedTable, function(a, b) return a[2] > b[2] end)
    local t1, t2, t3 = {}, {}, {}
    t1 = {getPlayerName(sortedTable[1][1]), sortedTable[1][2]} or {"N/A", "0"}
    t2 = {getPlayerName(sortedTable[2][1]), sortedTable[2][2]} or {"N/A", "0"} if sortedTable[2] then
    t3 = {getPlayerName(sortedTable[3][1]), sortedTable[3][2]} or {"N/A", "0"} if sortedTable[3] then
    triggerClientEvent(root, "showTopZDamage", root, t1, t2, t3)
end


This ensures that t2 and t3 are only assigned if the sortedTable array contains enough elements avoiding the error

Link to comment
 
function getTopThree()
    sortedTable = {}
    for player, damage in pairs(damageTable) do
        if isElement(player) then
            local team = getPlayerTeam(player)
                table.insert(sortedTable, {player, damage})
            end
        end
    table.sort(sortedTable, function(a, b) return a[2] > b[2] end)
    local t1, t2, t3 = {}, {}, {}
    t1 = {getPlayerName(sortedTable[1][1]), sortedTable[1][2]} or {"N/A", "0"}
    t2 = {getPlayerName(sortedTable[2][1]), sortedTable[2][2]} or {"N/A", "0"} if sortedTable[2] then
    t3 = {getPlayerName(sortedTable[3][1]), sortedTable[3][2]} or {"N/A", "0"} if sortedTable[3] then
    triggerClientEvent(root, "showTopZDamage", root, t1, t2, t3)
end
 

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...