Jump to content

Playtime


Sil3nT

Recommended Posts

This isnt working for Freeroam! The time isnt showing in column!

I add resource to acl and nothing hapened!

Server side:

exports.scoreboard:addScoreboardColumn( 'Play Time' ) 
  
local t = { } 
local uTimers = { } 
  
function checkValues( source,arg1,arg2 ) 
    if arg1 > 60 then 
        t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1 
        t[ source ][ 'sec' ] = 0 
    elseif arg2 > 3600 then 
        t[ source ][ 'sec' ] = 0 
        t[ source ][ 'min' ] = 0 
        t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1 
    end 
end 
  
function onPlayerLogOut ( _,account ) 
    if ( account ) and not isGuestAccount ( account ) then 
        local sValue = getElementData( source,'Play Time' ) 
        setAccountData ( account, "Play Time", tostring( sValue ) ) 
    end 
    if isTimer( uTimers[ source ] ) then 
        killTimer( uTimers[ source ] ) 
    end 
    uTimers[ source ] = nil 
    t[ source ] = nil 
end 
    
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Play Time' ) 
        setAccountData ( playeraccount, "Play Time", tostring( sValue ) ) 
    end 
    if isTimer( uTimers[ source ] ) then 
        killTimer( uTimers[ source ] ) 
    end 
    uTimers[ source ] = nil 
    t[ source ] = nil 
end 
  
function subFind( str,mod ) 
    if mod == 'hour' then 
        return str:sub( 1,str:find( ':' ) -1 ) 
    else 
        return str:sub( str:find( ':' ) + 1,str:len( ) ) 
    end 
end 
  
function onPlayerLogin (_, account ) 
    if account then 
        uTimers[ source ] = setTimer( 
            function( player,acc ) 
                local time = getAccountData ( acc, "Play Time" ) 
                if time then 
                    if not t[ player ] then 
                        t[ player ] = { 
                            ["hour"]    = tonumber( subFind( time,'hour' ) ), 
                            ["min"]     = tonumber( subFind( time ) ), 
                            ["sec"]     = 0 
                        } 
                    end 
                else 
                    if not t[ player ] then 
                        t[ player ] = { 
                            ["hour"]    = 0, 
                            ["min"]     = 0, 
                            ["sec"]     = 0 
                        } 
                    end 
                end 
                t[ player ][ 'sec' ] = tonumber( t[ player ][ 'sec' ] or 0 ) + 1 
                checkValues ( 
                    player, 
                    t[ player ][ 'sec' ] or 0, 
                    t[ player ][ 'min' ] or 0 
                )   
                setElementData ( 
                    player, 
                    "Play Time", 
                    tostring( t[ player ][ 'hour' ] or 0 )..':'..tostring( t[ player ][ 'min' ] or 0 ) 
                ) 
            end, 
        1000, 
        0 ,source,account ) 
    end 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 
addEventHandler( 'onPlayerLogout',root, onPlayerLogOut ) 

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