Jump to content

Stats


GanJaRuleZ

Recommended Posts

Hai allz , im always adding smth in my stats resource , but everytime it comes with an error , this time with 2 errors

So when i write /stats ganja it outputs :

  
*[sTATS] Player's userdata: 00000088 stats : -- Here it should be the name 
*Wins: 0! 
*Loses: 0! 
*Ratio: -1.#IND% ! --Here should be the ratio 

So here is the code , can someone fix it?

function DestructionWin( ) 
    local alivePlayers = getAlivePlayers( ) 
    if #alivePlayers == 1 then 
        local account = getPlayerAccount( alivePlayers[1] ) 
        local wins = getAccountData( account,"Race Wins" ) or 0 
        if account then 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Wins",tostring( wins ) +1 ) 
        end 
        local playername = getPlayerName( alivePlayers[1] ) 
        outputChatBox ( "#FFA824The player " .. playername .. " won!", root, 255, 255, 255, true ) 
    end 
end 
  
addEventHandler( "onPlayerWasted", root, 
    function( ) 
        local account = getPlayerAccount( source ) 
        local loses  = getAccountData( account,"Race Loses" ) or 0 
        if account then 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Loses",tostring( loses )+1 ) 
        end 
    end 
) 
  
function updateRatio (thePlayer) 
     local account = getPlayerAccount( theplayer ) 
     if account then 
     if isGuestAccount( account ) then return end 
     local wins = getAccountData( account, "Race Wins" ) or 0     
     local loses = getAccountData( account, "Race Loses" ) or 0 
    if loses == 0 then 
        setAccountData(account, "ratio", "-") 
    else 
        local kdr = round( wins / loses, 2) 
        setAccountData(account, "ratio", tostring(kdr)) 
    end 
end 
end 
  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            local ratio = getAccountData( account, "Ratio" ) or N/A 
            if loses and wins then 
                outputChatBox("#FFA824*" .. tostring( askdname ) .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. tostring( askdname ).. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
addEvent( "stats", true ) 
addEventHandler( "stats", root, publicstatsinfo ) 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
addEventHandler( "onPlayerChat", root, checkCommand ) 
  
function getPlayerFromNamePart(Name) 
if not (Name) then return end 
          for i,player in ipairs (getElementsByType ("player")) do 
          if (string.find(getPlayerName(player):lower(),Name:lower())) then 
          return player end 
     end 
end 
  
restriction = {} 
  
addCommandHandler("stats", 
function (thePlayer, cmd, whoToCheck) 
    local playerName = getPlayerName(thePlayer) 
    if restriction[playerName] then 
    outputChatBox("#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true) 
    else 
    restriction[playerName] = true 
    setTimer(restrictionEnd, 30000, 1, playerName) 
    local player = getPlayerFromNamePart( whoToCheck ) 
    if (player) then 
        local account = getPlayerAccount( player ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            ratio = (tonumber(wins)/tonumber(loses))*10 or N/A 
            outputChatBox("#FFA824*[sTATS] Player's "..tostring(player).." stats : " , root, 255, 12, 15, true) 
            if wins then 
                outputChatBox("#FFA824*Wins: "..tostring(wins).."!", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*Wins: 0  !" , root, 255, 12, 15, true) 
            end 
            if loses then 
                outputChatBox("#FFA824*Loses: "..tostring(loses).."!", root, 255, 12, 15, true) 
            else 
                outputChatBox("#FFA824*Loses: 0 !", root, 255, 12, 15, true) 
            end 
            if ratio then  
                outputChatBox("#FFA824*Ratio: "..tostring(ratio).."% !", root, 255, 12, 15, true) 
            else 
                outputChatBox("#FFA824*Ratio: - %", root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Player not found!" ) 
    end 
end 
end) 
  
function restrictionEnd(playerName) 
  restriction[playerName] = nil 
end  

I used the function : getPlayerFromNamePart from useful functions!

I used the ratio from 'score' resource ( idk how to make ratio , everytime i fail at it )

Link to comment

For calculate the ratio you can use this function(need the round function):

function calcRatio(val1, val2) 
    if (val1) and (val2) then 
        local val1= tonumber(val1) 
        local val2 = tonumber(val2) 
        if (val1) and (val2) then 
            if not (val1 == 0) and not (val2 == 0) then 
                if (val1 >= val2) then 
                    return 100 
                end 
                local ratio = round(((val1/val2)*100)) 
                return ratio 
            else 
                return 0 
            end 
        else 
            return false 
        end 
    else  
        return false 
    end 
end 

Link to comment
function DestructionWin( ) 
    local alivePlayers = getAlivePlayers( ) 
    if #alivePlayers == 1 then 
        local account = getPlayerAccount( alivePlayers[1] ) 
        local wins = getAccountData( account,"Race Wins" ) or 0 
        if account then 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Wins", wins + 1 ) 
        end 
        local playername = getPlayerName( alivePlayers[1] ) 
        outputChatBox ( "#FFA824The player " .. playername .. " won!", root, 255, 255, 255, true ) 
    end 
end 
  
addEventHandler( "onPlayerWasted", root, 
    function( ) 
        local account = getPlayerAccount( source ) 
        local loses  = getAccountData( account,"Race Loses" ) or 0 
        if account then 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Loses", loses + 1 ) 
        end 
    end 
) 
  
function updateRatio (thePlayer) 
     local account = getPlayerAccount( theplayer ) 
     if account then 
     if isGuestAccount( account ) then return end 
     local wins = getAccountData( account, "Race Wins" ) or 0     
     local loses = getAccountData( account, "Race Loses" ) or 0 
    if loses == 0 then 
        setAccountData(account, "ratio", "-") 
    else 
        local kdr = round( wins / loses, 2) 
        setAccountData(account, "ratio", kdr) 
    end 
end 
end 
  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            local ratio = getAccountData( account, "Ratio" ) or N/A 
            if loses and wins then 
                outputChatBox("#FFA824*" .. askdname .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. askdname .. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
addEvent( "stats", true ) 
addEventHandler( "stats", root, publicstatsinfo ) 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
addEventHandler( "onPlayerChat", root, checkCommand ) 
  
function getPlayerFromNamePart(Name) 
local thePlayer = getPlayerFromName(thePlayerName) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs(getElementsByType("player")) do 
        if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), thePlayerName:lower(), 1, true) then 
            return thePlayer 
        end 
    end 
return false 
end 
  
restriction = {} 
  
addCommandHandler("stats", 
function (thePlayer, cmd, whoToCheck) 
    local playerName = getPlayerName(thePlayer) 
    if restriction[playerName] then 
    outputChatBox("#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true) 
    else 
    restriction[playerName] = true 
    setTimer(restrictionEnd, 30000, 1, playerName) 
    local player = getPlayerFromNamePart( whoToCheck ) 
    if (player) then 
        local account = getPlayerAccount( player ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            ratio = (tonumber(wins)/tonumber(loses))*10 or N/A 
            outputChatBox("#FFA824*[sTATS] Player's "..tostring(player).." stats : " , root, 255, 12, 15, true) 
            if wins then 
                outputChatBox("#FFA824*Wins: "..tostring(wins).."!", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*Wins: 0  !" , root, 255, 12, 15, true) 
            end 
            if loses then 
                outputChatBox("#FFA824*Loses: "..tostring(loses).."!", root, 255, 12, 15, true) 
            else 
                outputChatBox("#FFA824*Loses: 0 !", root, 255, 12, 15, true) 
            end 
            if ratio then 
                outputChatBox("#FFA824*Ratio: "..tostring(ratio).."% !", root, 255, 12, 15, true) 
            else 
                outputChatBox("#FFA824*Ratio: - %", root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Player not found!" ) 
    end 
end 
end) 
  
function restrictionEnd(playerName) 
  restriction[playerName] = nil 
end 

I've changed your getPlayerFromNamePart whit another one better ^^

Link to comment
function DestructionWin( ) 
    local alivePlayers = getAlivePlayers( ) 
    if #alivePlayers == 1 then 
        local account = getPlayerAccount( alivePlayers[1] ) 
        local wins = getAccountData( account,"Race Wins" ) or 0 
        if account then 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Wins", wins + 1 ) 
        end 
        local playername = getPlayerName( alivePlayers[1] ) 
        outputChatBox ( "#FFA824The player " .. playername .. " won!", root, 255, 255, 255, true ) 
    end 
end 
  
addEventHandler( "onPlayerWasted", root, 
    function( ) 
        local account = getPlayerAccount( source ) 
        local loses  = getAccountData( account,"Race Loses" ) or 0 
        if account then 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Loses", loses + 1 ) 
        end 
    end 
) 
  
function updateRatio (thePlayer) 
     local account = getPlayerAccount( theplayer ) 
     if account then 
     if isGuestAccount( account ) then return end 
     local wins = getAccountData( account, "Race Wins" ) or 0     
     local loses = getAccountData( account, "Race Loses" ) or 0 
    if loses == 0 then 
        setAccountData(account, "ratio", "-") 
    else 
        local kdr = round( wins / loses, 2) 
        setAccountData(account, "ratio", kdr) 
    end 
end 
end 
  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            local ratio = getAccountData( account, "Ratio" ) or N/A 
            if loses and wins then 
                outputChatBox("#FFA824*" .. askdname .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. askdname .. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
addEvent( "stats", true ) 
addEventHandler( "stats", root, publicstatsinfo ) 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
addEventHandler( "onPlayerChat", root, checkCommand ) 
  
function getPlayerFromNamePart(thePlayerName) 
local thePlayer = getPlayerFromName(thePlayerName) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs(getElementsByType("player")) do 
        if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), thePlayerName:lower(), 1, true) then 
            return thePlayer 
        end 
    end 
return false 
end 
  
restriction = {} 
  
addCommandHandler("stats", 
function (thePlayer, cmd, whoToCheck) 
    local playerName = getPlayerName(thePlayer) 
    if restriction[playerName] then 
    outputChatBox("#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true) 
    else 
    restriction[playerName] = true 
    setTimer(restrictionEnd, 30000, 1, playerName) 
    local player = getPlayerFromNamePart( whoToCheck ) 
    if (player) then 
        local account = getPlayerAccount( player ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            ratio = (tonumber(wins)/tonumber(loses))*10 or N/A 
            outputChatBox("#FFA824*[sTATS] Player's "..tostring(player).." stats : " , root, 255, 12, 15, true) 
            if wins then 
                outputChatBox("#FFA824*Wins: "..tostring(wins).."!", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*Wins: 0  !" , root, 255, 12, 15, true) 
            end 
            if loses then 
                outputChatBox("#FFA824*Loses: "..tostring(loses).."!", root, 255, 12, 15, true) 
            else 
                outputChatBox("#FFA824*Loses: 0 !", root, 255, 12, 15, true) 
            end 
            if ratio then 
                outputChatBox("#FFA824*Ratio: "..tostring(ratio).."% !", root, 255, 12, 15, true) 
            else 
                outputChatBox("#FFA824*Ratio: - %", root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Player not found!" ) 
    end 
end 
end) 
  
function restrictionEnd(playerName) 
  restriction[playerName] = nil 
end 

Link to comment
Kenix: We all have different scripting ways.

This is not good believe me, I also wrote some long ago .

Just example:

function getPlayerFromNamePart(thePlayerName) 
local thePlayer = getPlayerFromName(thePlayerName) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs(getElementsByType("player")) do 
        if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), thePlayerName:lower(), 1, true) then 
            return thePlayer 
        end 
    end 
return false 
end 

function getPlayerFromNamePart( thePlayerName ) 
    local thePlayer = getPlayerFromName( thePlayerName ) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs( getElementsByType 'player' ) do 
        if string.find( string.gsub( getPlayerName( thePlayer ):lower( ),"#%x%x%x%x%x%x", "" ), thePlayerName:lower( ), 1, true ) then 
            return thePlayer 
        end 
    end 
    return false 
end 

What code better?

Link to comment
Look, you and nobody will tell me how to organize my scripts, understood? I don't want to sound rude but it look's like you're saying I must do it that way or die.

P.S: I didn't "tabulate" the code above because I just added a event handler.

What I wrote is not empty words ..

Think about it.

:wink:

Link to comment
restriction = {} 
  
function DestructionWin( ) 
    local alivePlayers = getAlivePlayers( ) 
    if #alivePlayers == 1 then 
        if isElement( alivePlayers[1] ) then 
            local account = getPlayerAccount( alivePlayers[1] ) 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Wins", tonumber( getAccountData( account,"Race Wins" ) or 0 ) + 1 ) 
            outputChatBox ( "#FFA824The player " .. getPlayerName( alivePlayers[1] ) .. " won!", root, 255, 255, 255, true ) 
        end  
    end 
end 
  
addEventHandler( "onPlayerWasted", root, 
    function( ) 
        local account = getPlayerAccount( source ) 
        if isGuestAccount( account ) then return end 
        setAccountData( account,"Race Loses", tonumber( getAccountData( account,"Race Loses" ) or 0 ) + 1 ) 
    end 
) 
  
function updateRatio ( thePlayer ) 
    if isElement( thePlayer ) then 
        local account = getPlayerAccount( theplayer ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Race Wins" ) or 0     
            local loses = getAccountData( account, "Race Loses" ) or 0 
            if loses == 0 then 
                setAccountData( account, "ratio", "-" ) 
            else 
                local kdr = round( wins / loses, 2 ) 
                setAccountData( account, "ratio", kdr ) 
            end 
        end 
    end  
end 
  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            local ratio = getAccountData( account, "Ratio" ) or 0 
            if loses and wins then 
                outputChatBox("#FFA824*" .. askdname .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. askdname .. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
addEvent( "stats", true ) 
addEventHandler( "stats", root, publicstatsinfo ) 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
addEventHandler( "onPlayerChat", root, checkCommand ) 
  
function getPlayerFromNamePart( thePlayerName ) 
    local thePlayer = getPlayerFromName( thePlayerName ) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs( getElementsByType 'player' ) do 
        if string.find( string.gsub( getPlayerName( thePlayer ):lower( ),"#%x%x%x%x%x%x", "" ), thePlayerName:lower( ), 1, true ) then 
            return thePlayer 
        end 
    end 
    return false 
end 
  
addCommandHandler("stats", 
    function ( thePlayer, cmd, whoToCheck ) 
        local playerName = getPlayerName( thePlayer ) 
        if restriction[ playerName ] then 
            outputChatBox( "#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true ) 
        else 
            restriction[ playerName ] = true 
            setTimer( restrictionEnd, 30000, 1, playerName ) 
            local player = getPlayerFromNamePart( whoToCheck ) 
            if player then 
                local account = getPlayerAccount( player ) 
                if account then 
                    if isGuestAccount( account ) then return end 
                    local wins = getAccountData( account, "Race Wins" ) or 0 
                    local loses = getAccountData( account, "Race Loses" ) or 0 
                    local ratio = ( tonumber( wins )/tonumber( loses ) )*10 
                    if tostring( ratio ):find '-nan' then 
                        ratio = 0 
                    end 
                    outputChatBox( "#FFA824*[sTATS] Player's "..tostring( getPlayerName( player ) ).." stats : " , root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Wins: "..tostring( wins ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Loses: "..tostring( loses ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Ratio: "..tostring( ratio ).."% !", root, 255, 12, 15, true ) 
                end 
            else 
                outputChatBox( "Player not found!" ) 
            end 
        end 
    end 
) 
  
function restrictionEnd( playerName ) 
  restriction[ playerName ] = nil 
end 

How you call function DestructionWin?

Link to comment
  
restriction = {} 
  
function DestructionWin( ) 
    local alivePlayers = getAlivePlayers( ) 
    if #alivePlayers == 1 then 
        if isElement( alivePlayers[1] ) then 
            local account = getPlayerAccount( alivePlayers[1] ) 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Wins", tonumber( getAccountData( account,"Race Wins" ) or 0 ) + 1 ) 
            outputChatBox ( "#FFA824The player " .. getPlayerName( alivePlayers[1] ) .. " won!", root, 255, 255, 255, true ) 
        end  
    end 
end 
addEventHandler( "onPlayerWasted", getRootElement(),DestructionWin) 
  
addEventHandler( "onPlayerWasted", root, 
    function( ) 
        local account = getPlayerAccount( source ) 
        if isGuestAccount( account ) then return end 
        setAccountData( account,"Race Loses", tonumber( getAccountData( account,"Race Loses" ) or 0 ) + 1 ) 
    end 
) 
  
function updateRatio ( thePlayer ) 
    if isElement( thePlayer ) then 
        local account = getPlayerAccount( theplayer ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Race Wins" ) or 0     
            local loses = getAccountData( account, "Race Loses" ) or 0 
            if loses == 0 then 
                setAccountData( account, "ratio", "-" ) 
            else 
                local kdr = round( wins / loses, 2 ) 
                setAccountData( account, "ratio", kdr ) 
            end 
        end 
    end  
end 
  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            local ratio = getAccountData( account, "Ratio" ) or 0 
            if loses and wins then 
                outputChatBox("#FFA824*" .. askdname .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. askdname .. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
addEvent( "stats", true ) 
addEventHandler( "stats", root, publicstatsinfo ) 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
addEventHandler( "onPlayerChat", root, checkCommand ) 
  
function getPlayerFromNamePart( thePlayerName ) 
    local thePlayer = getPlayerFromName( thePlayerName ) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs( getElementsByType 'player' ) do 
        if string.find( string.gsub( getPlayerName( thePlayer ):lower( ),"#%x%x%x%x%x%x", "" ), thePlayerName:lower( ), 1, true ) then 
            return thePlayer 
        end 
    end 
    return false 
end 
  
addCommandHandler("stats", 
    function ( thePlayer, cmd, whoToCheck ) 
        local playerName = getPlayerName( thePlayer ) 
        if restriction[ playerName ] then 
            outputChatBox( "#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true ) 
        else 
            restriction[ playerName ] = true 
            setTimer( restrictionEnd, 30000, 1, playerName ) 
            local player = getPlayerFromNamePart( whoToCheck ) 
            if player then 
                local account = getPlayerAccount( player ) 
                if account then 
                    if isGuestAccount( account ) then return end 
                    local wins = getAccountData( account, "Race Wins" ) or 0 
                    local loses = getAccountData( account, "Race Loses" ) or 0 
                    local ratio = ( tonumber( wins )/tonumber( loses ) )*10 
                    if tostring( ratio ):find '-nan' then 
                        ratio = 0 
                    end 
                    outputChatBox( "#FFA824*[sTATS] Player's "..tostring( getPlayerName( player ) ).." stats : " , root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Wins: "..tostring( wins ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Loses: "..tostring( loses ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Ratio: "..tostring( ratio ).."% !", root, 255, 12, 15, true ) 
                end 
            else 
                outputChatBox( "Player not found!" ) 
            end 
        end 
    end 
) 
  
function restrictionEnd( playerName ) 
  restriction[ playerName ] = nil 
end 

Works now , but now can someone give me ideas , when the player won , when he suicide , he wont get +1 To Race Loses..

EDIT : And the ratio is still 0 % ( when i do !stats or !st )

Link to comment
EDIT : And the ratio is still 0 % ( when i do !stats or !st )

0/0 = -nan

2/0 = inf

0/2 = 0

Understand?

You can try in lua demo

http://www.lua.org/cgi-bin/demo

And i create check this.if -nan or inf it created 0

Updated code.

addEvent( 'onPlayerRaceWasted',true ) 
  
restriction = {} 
  
function DestructionWin( vehicle ) 
    local alivePlayers = getAlivePlayers( ) 
    if #alivePlayers == 1 then 
        if isElement( alivePlayers[1] ) then 
            local account = getPlayerAccount( alivePlayers[1] ) 
            if isGuestAccount( account ) then return end 
            setAccountData( account,"Race Wins", tonumber( getAccountData( account,"Race Wins" ) or 0 ) + 1 ) 
            outputChatBox ( "#FFA824The player " .. getPlayerName( alivePlayers[1] ) .. " won!", root, 255, 255, 255, true ) 
        end  
    end 
end 
addEventHandler( "onPlayerRaceWasted", root,DestructionWin ) 
  
addEventHandler( "onPlayerWasted", root, 
    function( ) 
        local account = getPlayerAccount( source ) 
        if isGuestAccount( account ) then return end 
        setAccountData( account,"Race Loses", tonumber( getAccountData( account,"Race Loses" ) or 0 ) + 1 ) 
    end 
) 
  
function updateRatio ( thePlayer ) 
    if isElement( thePlayer ) then 
        local account = getPlayerAccount( theplayer ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Race Wins" ) or 0     
            local loses = getAccountData( account, "Race Loses" ) or 0 
            if loses == 0 then 
                setAccountData( account, "ratio", "-" ) 
            else 
                local kdr = round( wins / loses, 2 ) 
                setAccountData( account, "ratio", kdr ) 
            end 
        end 
    end  
end 
  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Race Wins" ) or 0 
            local loses = getAccountData( account, "Race Loses" ) or 0 
            local ratio = getAccountData( account, "Ratio" ) or 0 
            if loses and wins then 
                outputChatBox("#FFA824*" .. askdname .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. askdname .. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
addEvent( "stats", true ) 
addEventHandler( "stats", root, publicstatsinfo ) 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
addEventHandler( "onPlayerChat", root, checkCommand ) 
  
function getPlayerFromNamePart( thePlayerName ) 
    local thePlayer = getPlayerFromName( thePlayerName ) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs( getElementsByType 'player' ) do 
        if string.find( string.gsub( getPlayerName( thePlayer ):lower( ),"#%x%x%x%x%x%x", "" ), thePlayerName:lower( ), 1, true ) then 
            return thePlayer 
        end 
    end 
    return false 
end 
  
addCommandHandler("stats", 
    function ( thePlayer, cmd, whoToCheck ) 
        local playerName = getPlayerName( thePlayer ) 
        if restriction[ playerName ] then 
            outputChatBox( "#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true ) 
        else 
            restriction[ playerName ] = true 
            setTimer( restrictionEnd, 30000, 1, playerName ) 
            local player = getPlayerFromNamePart( whoToCheck ) 
            if player then 
                local account = getPlayerAccount( player ) 
                if account then 
                    if isGuestAccount( account ) then return end 
                    local wins = getAccountData( account, "Race Wins" ) or 0 
                    local loses = getAccountData( account, "Race Loses" ) or 0 
                    local ratio = ( tonumber( wins )/tonumber( loses ) )*10 
                    if tostring( ratio ):find '-nan' then 
                        ratio = 0 
                    elseif tostring( ratio ):find 'inf' then 
                        ratio = 0 
                    end 
                    outputChatBox( "#FFA824*[sTATS] Player's "..tostring( getPlayerName( player ) ).." stats : " , root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Wins: "..tostring( wins ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Loses: "..tostring( loses ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Ratio: "..tostring( ratio ).."% !", root, 255, 12, 15, true ) 
                end 
            else 
                outputChatBox( "Player not found!" ) 
            end 
        end 
    end 
) 
  
function restrictionEnd( playerName ) 
  restriction[ playerName ] = nil 
end 

Link to comment

What is a "ratio"? The percent of wins?

for example:

wins = 190

loses = 10

ratio = ( wins / loses ) * 10 -- result: 190, it's normal?

maybe this right for u:

ratio = math.max( 0, math.min( 100, math.floor( wins / ( loses + wins ) * 100 ) ) ) -- result: 95% 

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