Jump to content

Save player Kills and Deaths


knightscript

Recommended Posts

Hello, im trying to create a table where the players kills and deaths are saved, using this:

  
function playercreatekillsdeaths() 
    if not getElementData(source,"kills") then 
             setElementData(source,"kills",0) 
             outputDebugString("testing") 
    end 
    if not getElementData(source,"deaths") then 
             setElementData(source,"deaths",0) 
             outputDebugString("testing") 
    end 
end 
addEventHandler("onPlayerLogin", getRootElement(), playercreatekillsdeaths) 
  

To record when the player is killed im trying to use this:

function onplayerkilledplayer(source) 
thePlayer = getPlayerFromName (source) 
local count = getElementData(thePlayer,"kills") 
setElementData(thePlayer,"kills",count+1) 
end 
addEventHandler("onPlayerWasted", getRootElement(), onplayerkilledplayer) 

I really dont know how to achieve this.

Thanks

Link to comment

Alright, i found this script on the MTA resources page (https://community.multitheftauto.com/in ... ls&id=3472)

And it is working, except for 1 think, it is not showing the deaths on the table, and it shows no errors on the debugscript,

here is the code if its easier for you:

addEventHandler ( "onPlayerWasted", root, 
    function( totalAmmo, killer, killerWeapon, bodypart, stealth ) 
        if killer then 
            local account = getPlayerAccount ( killer ) 
            if killer ~= source then 
                setAccountData( account,"totalkillsdeaths.Kills",tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) or 0 ) +1 ) 
                setElementData( killer, "T/K", tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) ) ) 
            end  
        else 
            local accountSource = getPlayerAccount ( source ) 
            setAccountData( accountSource,"totalkillsdeaths.Deaths",tonumber( getAccountData(accountSource,"totalkillsdeaths.Deaths") or 0 ) +1 ) 
            setElementData( source, "T/D", tonumber( getAccountData( accountSource,"totalkillsdeaths.Deaths" ) ) ) 
        end 
    end 
)       
  
addEventHandler( "onPlayerLogin",root, 
    function( thePreviousAccount, theCurrentAccount, autoLogin ) 
        local account = getPlayerAccount ( source ) 
        if not getAccountData( account,"totalkillsdeaths.Kills" ) and not getAccountData( account,"totalkillsdeaths.Deaths" ) then 
            setAccountData( account,"totalkillsdeaths.Kills",0 ) 
            setAccountData( account,"totalkillsdeaths.Deaths",0 ) 
        end 
        setElementData( source,"T/D",tonumber( getAccountData( account,"totalkillsdeaths.Deaths" ) or 0 ) ) 
        setElementData( source,"T/K",tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) or 0 ) ) 
    end 
 ) 
  
addEventHandler( "onResourceStart",resourceRoot, 
    function( ) 
        outputDebugString( "add Total Kills to scoreboard Return: "..tostring( 
            call( getResourceFromName("Scoreboard"), "addScoreboardColumn", "T/K",root,2, 0.035 ) 
        ) ) 
        outputDebugString( "add Total Deaths to scoreboard Return: "..tostring( 
            call( getResourceFromName("Scoreboard"), "addScoreboardColumn", "T/D",root,3, 0.035 ) 
        ) ) 
    end 
) 

Thanks :)

Link to comment

I have modified the script a bit and it seems to work flawlessly.

-- Remove all the below comments to add kills and deaths ratio. 
  
peds = getElementsByType("ped") 
players = getElementsByType("player") 
addEventHandler ( "onPlayerWasted", root, 
    function( totalAmmo, killer, killerWeapon, bodypart, stealth ) 
        if killer == players and killer ~= peds and source == players and source ~= peds then 
            local account = getPlayerAccount ( killer ) 
            if killer ~= source then 
                setAccountData( account,"totalkillsdeaths.Kills",tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) or 0 ) +1 ) 
                setElementData( killer, "Kills", tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) ) ) 
               -- setElementData( killer, "Ratio", tonumber(getElementData( killer, "Kills" )/getElementData( source, "Deaths") ) ) 
            end  
        else 
            local accountSource = getPlayerAccount ( source ) 
            setAccountData( accountSource,"totalkillsdeaths.Deaths",tonumber( getAccountData(accountSource,"totalkillsdeaths.Deaths") or 0 ) +1 ) 
            setElementData( source, "Deaths", tonumber( getAccountData( accountSource,"totalkillsdeaths.Deaths" ) ) ) 
            --setElementData( source, "Ratio", tonumber(getElementData( killer, "Kills" )/getElementData( source, "Deaths") ) ) 
        end 
    end 
)       
  
addEventHandler( "onPlayerLogin",root, 
    function( thePreviousAccount, theCurrentAccount, autoLogin ) 
        local account = getPlayerAccount ( source ) 
        if not getAccountData( account,"totalkillsdeaths.Kills" ) and not getAccountData( account,"totalkillsdeaths.Deaths" ) then 
            setAccountData( account,"totalkillsdeaths.Kills",0 ) 
            setAccountData( account,"totalkillsdeaths.Deaths",0 ) 
        end 
        setElementData( source,"Deaths",tonumber( getAccountData( account,"totalkillsdeaths.Deaths" ) or 0 ) ) 
        setElementData( source,"Kills",tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) or 0 ) ) 
        --setElementData( source, "Ratio", getElementData( source, "Kills" )/getElementData( source, "Deaths") ) 
    end 
 ) 
  
addEventHandler( "onResourceStart",resourceRoot, 
    function( ) 
        --outputDebugString( "add Total Kills to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Kills',getRootElement(),4,50) 
        --) ) 
         
        --outputDebugString( "add Total Deaths to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Deaths',getRootElement(),5,50) 
        --) ) 
         
        --outputDebugString( "add Kills Deaths Ratio to scoreboard Return: "..tostring( 
            --exports.scoreboard:addScoreboardColumn('Ratio',getRootElement(),6,50) 
       -- ) ) 
    end 
) 

Link to comment

In above script, killing doesn't save when you kill your own self.

Test this one.

 -- Remove all the below comments to add kills and deaths ratio. 
  
peds = getElementsByType("ped") 
players = getElementsByType("player") 
addEventHandler ( "onPlayerWasted", root, 
    function( totalAmmo, killer, killerWeapon, bodypart, stealth ) 
        if killer == players and killer ~= peds and source == players and source ~= peds then 
            local account = getPlayerAccount ( killer ) 
            setAccountData( account,"totalkillsdeaths.Kills",tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) or 0 ) +1 ) 
            setElementData( killer, "Kills", tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) ) ) 
            -- setElementData( killer, "Ratio", tonumber(getElementData( killer, "Kills" )/getElementData( source, "Deaths") ) ) 
        else 
            local accountSource = getPlayerAccount ( source ) 
            setAccountData( accountSource,"totalkillsdeaths.Deaths",tonumber( getAccountData(accountSource,"totalkillsdeaths.Deaths") or 0 ) +1 ) 
            setElementData( source, "Deaths", tonumber( getAccountData( accountSource,"totalkillsdeaths.Deaths" ) ) ) 
            --setElementData( source, "Ratio", tonumber(getElementData( killer, "Kills" )/getElementData( source, "Deaths") ) ) 
        end 
    end 
)       
  
addEventHandler( "onPlayerLogin",root, 
    function( thePreviousAccount, theCurrentAccount, autoLogin ) 
        local account = getPlayerAccount ( source ) 
        if not getAccountData( account,"totalkillsdeaths.Kills" ) and not getAccountData( account,"totalkillsdeaths.Deaths" ) then 
            setAccountData( account,"totalkillsdeaths.Kills",0 ) 
            setAccountData( account,"totalkillsdeaths.Deaths",0 ) 
        end 
        setElementData( source,"Deaths",tonumber( getAccountData( account,"totalkillsdeaths.Deaths" ) or 0 ) ) 
        setElementData( source,"Kills",tonumber( getAccountData( account,"totalkillsdeaths.Kills" ) or 0 ) ) 
        --setElementData( source, "Ratio", getElementData( source, "Kills" )/getElementData( source, "Deaths") ) 
    end 
 ) 
  
addEventHandler( "onResourceStart",resourceRoot, 
    function( ) 
        --outputDebugString( "add Total Kills to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Kills',getRootElement(),4,50) 
        --) ) 
        
        --outputDebugString( "add Total Deaths to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Deaths',getRootElement(),5,50) 
        --) ) 
        
        --outputDebugString( "add Kills Deaths Ratio to scoreboard Return: "..tostring( 
            --exports.scoreboard:addScoreboardColumn('Ratio',getRootElement(),6,50) 
       -- ) ) 
    end 
) 

Link to comment

Try this one:

-- Remove all the below comments to add kills and deaths ratio. 
  
addEventHandler ( "onPlayerWasted", root, 
    function ( totalAmmo, killer, killerWeapon, bodypart, stealth ) 
        if ( killer ) then 
            local accountKiller = getPlayerAccount ( killer ) 
            if ( getElementType(killer) == "player" ) then 
                setAccountData ( accountKiller,"kills",tonumber ( getAccountData ( accountKiller,"kills" ) or 0 ) + 1 ) 
                setElementData ( killer, "kills", tonumber ( getAccountData ( accountKiller,"kills" ) ) ) 
            end 
        end 
        local accountSource = getPlayerAccount ( source ) 
        setAccountData ( accountSource, "Deaths",tonumber ( getAccountData ( accountSource, "Deaths" ) or 0 ) +1 ) 
        setElementData ( source, "Deaths", tonumber ( getAccountData ( accountSource, "Deaths" ) ) ) 
    end 
) 
addEventHandler( "onPlayerLogin",root, 
    function( thePreviousAccount, theCurrentAccount, autoLogin ) 
        local account = getPlayerAccount ( source ) 
        if not getAccountData( account,"Kills" ) and not getAccountData( account,"Deaths" ) then 
            setAccountData( account,"Kills",0 ) 
            setAccountData( account,"Deaths",0 ) 
        end 
        setElementData( source,"Deaths",tonumber( getAccountData( account,"Deaths" ) or 0 ) ) 
        setElementData( source,"Kills",tonumber( getAccountData( account,"Kills" ) or 0 ) ) 
        --setElementData( source, "Ratio", getElementData( source, "Kills" )/getElementData( source, "Deaths") ) 
    end 
 ) 
  
addEventHandler( "onResourceStart",resourceRoot, 
    function( ) 
        --outputDebugString( "add Total Kills to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Kills',getRootElement(),4,50) 
        --) ) 
         
        --outputDebugString( "add Total Deaths to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Deaths',getRootElement(),5,50) 
        --) ) 
         
        --outputDebugString( "add Kills Deaths Ratio to scoreboard Return: "..tostring( 
            --exports.scoreboard:addScoreboardColumn('Ratio',getRootElement(),6,50) 
       -- ) ) 
    end 
) 

Link to comment
Try this one:
-- Remove all the below comments to add kills and deaths ratio. 
  
addEventHandler ( "onPlayerWasted", root, 
    function ( totalAmmo, killer, killerWeapon, bodypart, stealth ) 
        if ( killer ) then 
            local accountKiller = getPlayerAccount ( killer ) 
            if ( getElementType(killer) == "player" ) then 
                setAccountData ( accountKiller,"kills",tonumber ( getAccountData ( accountKiller,"kills" ) or 0 ) + 1 ) 
                setElementData ( killer, "kills", tonumber ( getAccountData ( accountKiller,"kills" ) ) ) 
            end 
        end 
        local accountSource = getPlayerAccount ( source ) 
        setAccountData ( accountSource, "Deaths",tonumber ( getAccountData ( accountSource, "Deaths" ) or 0 ) +1 ) 
        setElementData ( source, "Deaths", tonumber ( getAccountData ( accountSource, "Deaths" ) ) ) 
    end 
) 
addEventHandler( "onPlayerLogin",root, 
    function( thePreviousAccount, theCurrentAccount, autoLogin ) 
        local account = getPlayerAccount ( source ) 
        if not getAccountData( account,"Kills" ) and not getAccountData( account,"Deaths" ) then 
            setAccountData( account,"Kills",0 ) 
            setAccountData( account,"Deaths",0 ) 
        end 
        setElementData( source,"Deaths",tonumber( getAccountData( account,"Deaths" ) or 0 ) ) 
        setElementData( source,"Kills",tonumber( getAccountData( account,"Kills" ) or 0 ) ) 
        --setElementData( source, "Ratio", getElementData( source, "Kills" )/getElementData( source, "Deaths") ) 
    end 
 ) 
  
addEventHandler( "onResourceStart",resourceRoot, 
    function( ) 
        --outputDebugString( "add Total Kills to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Kills',getRootElement(),4,50) 
        --) ) 
         
        --outputDebugString( "add Total Deaths to scoreboard Return: "..tostring( 
            exports.scoreboard:addScoreboardColumn('Deaths',getRootElement(),5,50) 
        --) ) 
         
        --outputDebugString( "add Kills Deaths Ratio to scoreboard Return: "..tostring( 
            --exports.scoreboard:addScoreboardColumn('Ratio',getRootElement(),6,50) 
       -- ) ) 
    end 
) 

setAccountData save strings only

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