Jump to content

Bugged Stats system


PicoPr0

Recommended Posts

here is my stats system

addEventHandler("onPlayerLogin",getRootElement(), 
function () 
    local account = getPlayerAccount(source) 
    local kills = getAccountData(account,"kills") 
    local deaths = getAccountData(account,"deaths") 
    local headshots = getAccountData(account,"headshots") 
    if not kills then setAccountData(account,"kills",0) end 
    if not deaths then setAccountData(account,"deaths",0) end 
    if not headshots then setAccountData(account,"headshots",0) end 
end) 
  
function updatePlayerStats(ammo, attacker, weapon, bodypart) 
    local account = getPlayerAccount(attacker) 
    if account then 
        local kills = getAccountData(account,"kills") 
        local headshots = getAccountData(account,"headshots") 
        setAccountData(account,"kills",kills+1) 
        if bodypart == 9 then 
            setAccountData(account,"headshots",headshots+1) 
        end 
    end 
    end 
    if not attacker or attacker == source then return end 
    local accountS = getPlayerAccount(source) 
    if accountS then 
        local deaths = getAccountData(accountS,"deaths") 
        setAccountData(accountS,"deaths",deaths+1) 
        end 
end 
addEventHandler("onPlayerWasted",getRootElement(),updatePlayerStats) 
  
function getPlayerStats(thePlayer) 
    local account = getPlayerAccount(thePlayer) 
    if account then 
        local kills = getAccountData(account,"kills") or 0 
        local headshots = getAccountData(account,"headshots") or 0 
        local deaths = getAccountData(account,"deaths") or 0 
        local ratio = string.format("%.2f", kills / deaths) 
        outputChatBox("[sTATS]".. getPlayerName(thePlayer) .."'s Stats: Kills: ".. tostring(kills) .." (".. tostring(headshots) .." Headshots), ".. tostring(deaths) .." Deaths, Ratio: ".. tostring(ratio).."", getRootElement(), 50, 255, 0) 
    end 
end 
addCommandHandler("stats",getPlayerStats) 
addCommandHandler("st",getPlayerStats) 

I tested it after every round it says 1 Death more without getting killed .

why ? :o

Link to comment

Well, I've found a extra "end " in the "onPlayerWasted function,

try this:

addEventHandler("onPlayerLogin",root,function() 
    local account = getPlayerAccount(source) 
    local kills = getAccountData(account,"kills") 
    local deaths = getAccountData(account,"deaths") 
    local headshots = getAccountData(account,"headshots") 
    if not kills then setAccountData(account,"kills",0) end 
    if not deaths then setAccountData(account,"deaths",0) end 
    if not headshots then setAccountData(account,"headshots",0) end 
end) 
  
function updatePlayerStats(ammo, attacker, weapon, bodypart) 
    local account = getPlayerAccount(attacker) 
    if account then 
        local kills = getAccountData(account,"kills") 
        local headshots = getAccountData(account,"headshots") 
        setAccountData(account,"kills",kills+1) 
        if bodypart == 9 then 
            setAccountData(account,"headshots",headshots+1) 
        end 
    end 
    if not attacker or attacker == source then return end 
    local accountS = getPlayerAccount(source) 
    if accountS then 
        local deaths = getAccountData(accountS,"deaths") 
        setAccountData(accountS,"deaths",deaths+1) 
    end 
end 
addEventHandler("onPlayerWasted",getRootElement(),updatePlayerStats) 
  
function getPlayerStats(thePlayer) 
    local account = getPlayerAccount(thePlayer) 
    if account then 
        local kills = getAccountData(account,"kills") or 0 
        local headshots = getAccountData(account,"headshots") or 0 
        local deaths = getAccountData(account,"deaths") or 0 
        local ratio = string.format("%.2f", kills / deaths) 
        outputChatBox("[sTATS]".. getPlayerName(thePlayer) .."'s Stats: Kills: ".. tostring(kills) .." (".. tostring(headshots) .." Headshots), ".. tostring(deaths) .." Deaths, Ratio: ".. tostring(ratio).."", getRootElement(), 50, 255, 0) 
    end 
end 
addCommandHandler("stats",getPlayerStats) 
addCommandHandler("st",getPlayerStats) 

Link to comment
function updatePlayerStats ( ammo, attacker, weapon, bodypart ) 
    if ( not attacker or attacker == source ) then 
        return 
    end 
    local account = getPlayerAccount ( attacker ) 
    if ( account and not isGuestAccount ( account ) ) then 
        local kills = tonumber ( getAccountData ( account, "kills" ) ) or 0 
        local headshots = tonumber ( getAccountData ( account, "headshots" ) ) or 0 
        setAccountData ( account, "kills", kills + 1 ) 
        if ( bodypart == 9 ) then 
            setAccountData ( account, "headshots", headshots + 1 ) 
        end 
    end 
    local accountS = getPlayerAccount ( source ) 
    if ( accountS and not isGuestAccount ( accountS ) ) then 
        local deaths = tonumber ( getAccountData ( accountS, "deaths" ) ) or 0 
        setAccountData ( accountS, "deaths", deaths + 1 ) 
    end 
end 
addEventHandler ( "onPlayerWasted", root, updatePlayerStats ) 
  
function getPlayerStats ( thePlayer ) 
    local account = getPlayerAccount ( thePlayer ) 
    if ( account and not isGuestAccount ( account ) ) then 
        local kills = getAccountData ( account, "kills" ) or 0 
        local headshots = getAccountData ( account, "headshots" ) or 0 
        local deaths = getAccountData ( account, "deaths" ) or 0 
        local ratio = string.format ( "%.2f", kills / deaths ) 
        outputChatBox ( "[sTATS]".. getPlayerName ( thePlayer ) .."'s Stats: Kills: ".. tostring ( kills ) .." (".. tostring ( headshots ) .." Headshots), ".. tostring ( deaths ) .." Deaths, Ratio: ".. tostring ( ratio ), root, 50, 255, 0 ) 
    end 
end 
addCommandHandler ( "stats", getPlayerStats ) 
addCommandHandler ( "st", getPlayerStats ) 

Edited by Guest
Link to comment
  
  
function fUpdatePlayerStats ( _, uAttacker, _, nBodypart ) 
    if ( not uAttacker or uAttacker == source ) then 
        return 
    end 
    local uAccount = getPlayerAccount ( uAttacker ) 
    if ( uAccount and not isGuestAccount ( uAccount ) ) then 
        local nKills = tonumber ( getAccountData ( uAccount, "kills" )  ) or 0 
        local nHeadshots = tonumber ( getAccountData ( uAccount, "headshots" ) ) or 0 
        setAccountData ( uAccount, "kills", nKills + 1 ) 
        if ( nBodypart == 9 ) then 
            setAccountData ( uAccount, "headshots", nHeadshots + 1 ) 
        end 
    end 
    local uAccountSource = getPlayerAccount ( source ) 
    if ( uAccountSource and not isGuestAccount ( uAccountSource ) ) then 
        local nDeaths = tonumber ( getAccountData ( uAccountSource, "deaths" ) ) or 0 
        setAccountData ( uAccountSource, "deaths", nDeaths + 1 ) 
    end 
end 
  
function fGetPlayerStats ( uPlayer ) 
    local uAccount = getPlayerAccount ( uPlayer ) 
    if ( uAccount and not isGuestAccount ( uAccount ) ) then 
        local nKills = getAccountData ( uAccount, "kills" ) or 0 
        local nHeadshots = getAccountData ( uAccount, "headshots" ) or 0 
        local nDeaths = getAccountData ( uAccount, "deaths" ) or 0 
        local nRatio = math.max( 0, math.min( 100, math.floor( nKills / ( nDeaths + nKills ) * 100 ) ) ) 
        outputChatBox ( "[sTATS]".. getPlayerName ( uPlayer ) .."'s Stats: Kills: ".. tostring ( nKills ) .." (".. tostring ( nHeadshots ) .." Headshots), ".. tostring ( deaths ) .." Deaths, Ratio: ".. tostring ( nRatio ), root, 50, 255, 0 ) 
    end 
end 
  
addEventHandler     ( "onPlayerWasted", root,   fUpdatePlayerStats  ) 
addCommandHandler   ( "stats",                  fGetPlayerStats     ) 
addCommandHandler   ( "st",                     fGetPlayerStats     ) 
  

Updated.

SolidSnake14,Your code have syntax errors and ratio calculation wrong too.

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