Nameless Posted July 29, 2012 Share Posted July 29, 2012 Hello, Can anybody tell me how to save the time played for a player? I need to start the time count when somebody logs in and stop the time count when he logs out or quits. (in a sqlite database) I hope you guys can help me! //Nameless Link to comment
Anderl Posted July 29, 2012 Share Posted July 29, 2012 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
denny199 Posted July 29, 2012 Share Posted July 29, 2012 or just this: https://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 Link to comment
_Dark_ Posted July 29, 2012 Share Posted July 29, 2012 or just this:https://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. Link to comment
denny199 Posted July 29, 2012 Share Posted July 29, 2012 or just this:https://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
_Dark_ Posted July 29, 2012 Share Posted July 29, 2012 denny199, I didn't say anything to you personally. Link to comment
robhol Posted July 29, 2012 Share Posted July 29, 2012 Does everything have to become some kind of quarrel around here? Dark is right - doing it one second at a time is stupid. Let it drop. Link to comment
denny199 Posted July 29, 2012 Share Posted July 29, 2012 pfffffff, only just helping someone and then getting a negative comment wow, nice. I'm out off this Link to comment
Nameless Posted July 30, 2012 Author Share Posted July 30, 2012 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
'LinKin Posted February 6, 2014 Share Posted February 6, 2014 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
myonlake Posted February 6, 2014 Share Posted February 6, 2014 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now