Jump to content

Player On Server Time


Rob0

Recommended Posts

I've seen this resource elsewhere. In the scoreboard it displays the players total time on server. If this is public can someone name what I'm looking for? Otherwise I need a pointer to write it. I need the lua/mta equivalent of $ctime. A number fixed to some pointless date based on seconds that i can use to calculate elapsed time.

Link to comment

It's kinda easy mate, I can write it for you:

local columnName = "Online time"; 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        exports.scoreboard:scoreboardAddColumn ( columnName ); 
         
        for _, player in pairs ( getElementsByType ( "player" ) ) do 
            setElementData ( player, "global:time", getTickCount ( ) ); 
        end 
    end 
); 
  
addEventHandler ( "onPlayerLogin", root, 
    function ( _, account ) 
        setElementData ( source, "global:time", getTickCount ( ) ); 
    end 
); 
  
addEventHandler ( "onPlayerQuit", root, 
    function ( ) 
        local time = getElementData ( source, "global:time" ); 
        local timeSpentOnServer = ( getTickCount ( ) - time ) / 1000; 
         
        local account = getPlayerAccount ( source ) 
        if ( account and not isGuestAccount ( account ) ) then 
            local data = getAccountData ( account, "global:time" ); 
            if ( data ) then 
                setAccountData ( account, "global:time", data + timeSpentOnServer ); 
            else 
                setAccountData ( account, "global:time", timeSpentOnServer ); 
            end 
        end 
    end 
); 
  
setTimer ( 
    function ( ) 
        for _, player in pairs ( getElementsByType ( "player" ) ) do 
            local startTime = getElementData ( player, "global:time" ); 
            if ( startTime ) then 
                local onlineTime = ( getTickCount - startTime ) / 1000; 
                setElementData ( player, columnName, string.format ( "%.2d:%.2d:%.2d", onlineTime / (60*60), onlineTime / 60 %6 0, onlineTime % 60 ) ); 
            end 
        end 
    end, 
500, 0 ); 

Should work. Not tested.

Link to comment

could you give this line a second look please it is giving me trouble

  
setElementData ( player, columnName, string.format ( "%.2d:%.2d:%.2d", onlineTime / (60*60), onlineTime / 60 %6 0, onlineTime % 60 ) ); 

')' expected near '0'

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