Jump to content

Save Playtime


Karuzo

Recommended Posts

Hey Guys,

I wanted to get the playtime of the player if the resource restarts and save it if the resource will be stopped,

i've found this resource in the community:

but idk which event i should use to save it.

Maybe someone could help me ?

//EDIT: And if one minute is over it goes to the Minute Section, e.g: 00:23:1 ( it stays 1, this is the seconds section, and 23 is now the seconds section but that should be minutes, why ? ))

Client:

  
local playtime = tostring(getElementData(player, "Online Time")) or 0--This is how i get it and put it in my scoreboard(i use my scoreboard not the normal one.) 

Server:

  
  
  
local t = { } 
  
function checkValues( source,arg1,arg2) 
    if (arg2 >= 60) then 
        t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1 
        t[ source ][ 'sec' ] = 0 
    end 
    if (arg1 >= 60) then 
        t[ source ][ 'min' ] = 0 
        t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1 
    end 
    return arg1, arg2 
end 
      
setTimer( 
    function( ) 
        for _, v in pairs( getElementsByType( "player" ) ) do 
            if (not t[ v ]) then 
                t[ v ] = { 
                            ["hour"] = 0, 
                             ["min"] = 0, 
                             ["sec"] = 0 
                            } 
            end 
  
            t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1 
            local min,sec = checkValues ( 
                    v, 
                    t[ v ][ 'min' ] or 0, 
                    t[ v ][ 'sec' ] or 0 
                        )   
    local hour = tonumber( t[ v ][ 'hour' ] or 0 ) 
  
            setElementData( 
                v, 
                "Online Time", 
                tostring( hour )..':'..tostring( min )..':'..tostring( sec ) 
            ) 
        end 
    end, 
    1000, 0 
) 
    
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Online Time' ) 
        setAccountData ( playeraccount, "Online Time", tostring(sValue) ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Online Time" ) 
        if ( time ) then 
            setElementData ( source, "Online Time", time ) 
                else 
            setElementData ( source, "Online Time",0 ) 
        setAccountData ( playeraccount, "Online Time",0 ) 
        end 
    end 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 

Link to comment

No Way to save it with elementdatas like with onResourceStart or smth like that ?

And what about this problem:

And if one minute is over it goes to the Minute Section, e.g: 00:23:1 ( it stays 1, this is the seconds section, and 23 is now the seconds section but that should be minutes, why ? ))

//EDIT: Fixed ^this problem.

But i can't save it.

Link to comment

Just use this!

Its work for me and saves everything, but this script dont have seconds!

exports [ "scoreboard" ]:addScoreboardColumn ( "Online", 3 ) 
  
addEventHandler ( "onResourceStart" , resourceRoot , 
    function ( ) 
        for index , player in ipairs ( getElementsByType ( "player" ) ) do 
            local pAccount = getPlayerAccount ( player ) 
            if not isGuestAccount ( pAccount ) then 
                local minutes = getAccountData ( pAccount , "Online.minutes" ) 
                if minutes then 
                    local hours = getAccountData ( pAccount , "Online.hours" ) 
                    if # tostring ( minutes ) == 1 then 
                        minutes = "0" .. minutes 
                    end 
                    if # tostring ( hours ) == 1 then 
                        hours = "0" .. hours 
                    end 
                    setElementData ( player , "Online" , hours .. ":" .. minutes .. ":00" ) 
                    local timer = setTimer ( updatePlayerOnline , 60000 , 1 , player ) 
                    setElementData ( player , "Online.timer" , timer ) 
                else 
                    setAccountData ( pAccount , "Online.minutes" , 0 ) 
      
              setAccountData ( pAccount , "Online.hours" , 0 ) 
                    setElementData ( player , "Online" , "00:00:00" ) 
                    local timer = setTimer ( updatePlayerOnline , 60000 , 1 , player ) 
                    setElementData ( player , "Online.timer" , timer ) 
                end 
            else 
                setElementData ( player , "Online" , "N/A" ) 
            end 
        end 
    end 
) 
  
addEventHandler ( "onResourceStop" , resourceRoot , 
    function ( ) 
        for index , player in ipairs ( getElementsByType ( "player" ) ) do 
            local pAccount = getPlayerAccount ( player ) 
            if not isGuestAccount ( pAccount ) then 
                local timer = getElementData ( player , "Online.timer" ) 
                if isTimer ( timer ) then 
                    killTimer ( timer ) 
                end 
            end 
        end 
    end 
) 
  
addEventHandler ( "onPlayerLogin" , root , 
    function ( _ , pAccount ) 
        local minutes = getAccountData ( pAccount , "Online.minutes" ) 
        if minutes then 
            local hours = getAccountData ( pAccount , "Online.hours" ) 
            if # tostring ( minutes ) == 1 then 
                minutes = "0" .. minutes 
            end 
            if # tostring ( hours ) == 1 then 
                hours = "0" .. hours 
            end 
            setElementData ( source , "Online" , hours .. ":" .. minutes .. ":00" ) 
            local timer = setTimer ( updatePlayerOnline , 60000 , 1 , source ) 
            setElementData ( source , "Online.timer" , timer ) 
        else 
            setAccountData ( pAccount , "Online.minutes" , 0 ) 
            setAccountData ( pAccount , "Online.hours" , 0 ) 
            setElementData ( source , "Online" , "00:00:00" ) 
            local timer = setTimer ( updatePlayerOnline , 60000 , 1 , source ) 
            setElementData ( source , "Online.timer" , timer ) 
        end 
    end 
) 
  
addEventHandler ( "onPlayerLogout" , root , 
    function ( pAccount ) 
        local timer = getElementData ( source , "Online.timer" ) 
        if isTimer ( timer ) then 
            killTimer ( timer ) 
        end 
    end 
) 
  
addEventHandler ( "onPlayerJoin" , root , 
    function ( ) 
        setElementData ( source , "Online" , "N/A" ) 
    end 
) 
  
addEventHandler ( "onPlayerQuit" , root , 
    function ( ) 
        local pAccount = getPlayerAccount ( source ) 
        if not isGuestAccount ( pAccount ) then 
            local timer = getElementData ( source , "Online.timer" ) 
            if isTimer ( timer ) then 
                killTimer ( timer ) 
            end 
        end 
    end 
) 
  
function updatePlayerOnline ( player ) 
    local pAccount = getPlayerAccount ( player ) 
    local minutes = getAccountData ( pAccount , "Online.minutes" ) 
    local hours = getAccountData ( pAccount , "Online.hours" ) 
    minutes = tostring ( tonumber ( minutes ) + 1 ) 
    if minutes == "60" then 
        hours = tostring ( tonumber ( hours ) + 1 ) 
        minutes = "00" 
    end 
    setAccountData ( pAccount , "Online.minutes" , tonumber ( minutes ) ) 
    setAccountData ( pAccount , "Online.hours" , tonumber ( hours ) ) 
    if # tostring ( minutes ) == 1 then minutes = "0" .. minutes end 
    if # tostring ( hours ) == 1 then hours = "0" .. hours end 
    setElementData ( player , "Online" , hours .. ":" .. minutes .. ":00" ) 
    local timer = setTimer ( updatePlayerOnline , 60000 , 1 , player ) 
    setElementData ( player , "Online.timer" , timer ) 
end 

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