Jump to content

Save Playtime


Nameless

Recommended Posts

local aPlayerTime = { } 
  
addEventHandler( 'onPlayerLogin', root, 
    function( ) 
        if( not aPlayerTime[ source ] ) then 
            aPlayerTime[ source ] = { iTime = getTickCount( ), bLoggedOut = false } 
        end 
    end 
) 
  
function getPlayerPlayTime( pPlayer ) 
    if( aPlayerTime[ pPlayer ] ) then 
        -- convert player play time from ms to hour:minute:second 
        return --[[ time converted ]] aPlayerTime[ pPlayer ] 
    end 
    return false 
end 
  
addEventHandler( 'onPlayerLogout', root, 
    function( ) 
        local endTime = getTickCount( ) 
        aPlayerTime[ source ].iTime = aPlayerTime[ source ].iTime - endTime 
        aPlayerTime[ source ].bLoggedOut = true 
    end  
) 
  
addEventHandler( 'onPlayerQuit', root, 
    function( ) 
        if( aPlayerTime[ source ].bLoggedOut ) then 
            return 
        end 
         
        local endTime = getTickCount( ) 
        aPlayerTime[ source ].iTime = aPlayerTime[ source ].iTime - endTime 
    end 
) 

Now edit it to SQL.

Link to comment
or just this:

http://community.multitheftauto.com/ind ... ls&id=4784

Searching is a good way to find good stuff

But when your'e searching you'll never learn it ;-)

1 second timer for updating player game time? Bad.

that isn't my script.

... I'm here for helping not for discussing, for the second time:

Searching is a good way to find good stuff

But when your'e searching you'll never learn it ;-)

gosh, why replying man?

you can help me too, without be replaying on my posts, with negative comments.

Link to comment
local aPlayerTime = { } 
  
addEventHandler( 'onPlayerLogin', root, 
    function( ) 
        if( not aPlayerTime[ source ] ) then 
            aPlayerTime[ source ] = { iTime = getTickCount( ), bLoggedOut = false } 
        end 
    end 
) 
  
function getPlayerPlayTime( pPlayer ) 
    if( aPlayerTime[ pPlayer ] ) then 
        -- convert player play time from ms to hour:minute:second 
        return --[[ time converted ]] aPlayerTime[ pPlayer ] 
    end 
    return false 
end 
  
addEventHandler( 'onPlayerLogout', root, 
    function( ) 
        local endTime = getTickCount( ) 
        aPlayerTime[ source ].iTime = aPlayerTime[ source ].iTime - endTime 
        aPlayerTime[ source ].bLoggedOut = true 
    end  
) 
  
addEventHandler( 'onPlayerQuit', root, 
    function( ) 
        if( aPlayerTime[ source ].bLoggedOut ) then 
            return 
        end 
         
        local endTime = getTickCount( ) 
        aPlayerTime[ source ].iTime = aPlayerTime[ source ].iTime - endTime 
    end 
) 

Now edit it to SQL.

Thank you very much Anderl!

Link to comment
  • 1 year later...

Hey,

so I'm using the guy's code

local aPlayerTime = { } 
  
addEventHandler( 'onPlayerLogin', root, 
    function( ) 
        if( not aPlayerTime[ source ] ) then 
            aPlayerTime[ source ] = { iTime = getTickCount( ), bLoggedOut = false } 
        end 
    end 
) 
  
function getPlayerPlayTime( pPlayer ) 
    if( aPlayerTime[ pPlayer ] ) then 
        -- convert player play time from ms to hour:minute:second 
        return --[[ time converted ]] aPlayerTime[ pPlayer ] 
    end 
    return false 
end 
  
addEventHandler( 'onPlayerLogout', root, 
    function( ) 
        local endTime = getTickCount( ) 
        aPlayerTime[ source ].iTime = aPlayerTime[ source ].iTime - endTime 
        aPlayerTime[ source ].bLoggedOut = true 
    end 
) 
  
addEventHandler( 'onPlayerQuit', root, 
    function( ) 
        if( aPlayerTime[ source ].bLoggedOut ) then 
            return 
        end 
        
        local endTime = getTickCount( ) 
        aPlayerTime[ source ].iTime = aPlayerTime[ source ].iTime - endTime 
    end 
) 

But I got one question.

In the table aPlayerTime, you insert a position (aPlayerTime[ source ] = { iTime = getTickCount( ), bLoggedOut = false })

So the question is, as soon as a player quits the server, is that row deleted from the table?

I don't understand very well they way it works.

Link to comment
  • MTA Team

No, it does not. For that reason you should instead of using player element as array key, use a unique key like account ID or so. This way you can always get the saved player time without getting an error of undefined element after the player has quit.

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