Jump to content

[help]save time in scoreboard


#RooTs

Recommended Posts

Hello, I have a problem with this my script, he is not saving my playing time in the database.

when I go out of the game or even the logout, someone help me?

jyrBGCR.png

  
call(getResourceFromName("scoreboard"), "addScoreboardColumn", "Tempo jogado", getRootElement(), 8, 0.16) 
  
  
local t = { } 
  
function checkValues( source,arg1,arg2) 
    if (arg2 >= 59) then 
        t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1 
        t[ source ][ 'sec' ] = 0 
    end 
    if (arg1 >= 59) 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, 
                "Tempo jogado", 
                "h: "..string.format("%02d",tostring( hour ))..' m: '..string.format("%02d",tostring( min ))..' s: '..string.format("%02d",tostring( sec )) 
            ) 
        end 
    end, 
    1000, 0 
) 
    
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tempo jogado' ) 
        setAccountData ( playeraccount, "Tempo jogado", tostring(sValue) ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Tempo jogado" ) 
        if ( time ) then 
            setElementData ( source, "Tempo jogado", time ) 
                else 
            setElementData ( source, "Tempo jogado",0 ) 
        setAccountData ( playeraccount, "Tempo jogado",0 ) 
        end 
    end 
end 
  
  
  
function onPlayerLogOut ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tempo jogado' ) 
        setAccountData ( playeraccount, "Tempo jogado", tostring(sValue) ) 
    end 
    t[ source ] = nil 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 
addEventHandler( 'onPlayerLogout',root, onPlayerLogOut ) 

Link to comment

Little speed up:

  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Tempo jogado" ) or 0 
            setElementData ( source, "Tempo jogado",time ) 
            setAccountData ( playeraccount, "Tempo jogado",time ) 
    end 
end 
  
  

Now... coming to the issue.

  
  
            setElementData( 
                v, 
                "Tempo jogado", 
                "h: "..string.format("%02d",tostring( hour ))..' m: '..string.format("%02d",tostring( min ))..' s: '..string.format("%02d",tostring( sec )) 
            ) 
  
  

Just use a second-counter, and then do the formatting client-side, to make it easier and avoid mistakes.

Link to comment

Oh yeah, didnt saw it was scoreboard..

Try this:

  
call(getResourceFromName("scoreboard"), "addScoreboardColumn", "Tempo jogado", getRootElement(), 8, 0.16) 
  
  
local t = { } 
local cv = {} 
  
function checkValues( source,arg1,arg2,arg3) 
    if (not source) or (not isElement(source)) then return end 
    if getElementType(source) ~= "player" then return end 
    local arg1,arg2,arg3 = arg1 or 0, arg2 or 0, arg3 or 0 
    if (arg1 >= 59) then 
    arg2 = arg2+1 
    arg1 = 0 
    end 
    if (arg2 >= 59) then 
    arg3 = arg3+1 
    arg2 = 0 
    end 
    return arg1, arg2,arg3 
end 
      
setTimer( 
    function( ) 
        for _, v in pairs( getElementsByType( "player" ) ) do 
            if (not t[ v ]) then 
                t[ v ] = { 
                            ["hour"] = 0, 
                             ["min"] = 0, 
                             ["sec"] = 0 
                            } 
            end 
            local sec = t[v].sec or 0 
            sec = sec + 1 
            local min = t[v].min or 0 
            local hour = t[v].hour or 0 
             
            local sec,min,hour = checkValues ( 
                    v, 
                    sec, 
                    min, 
                    hour 
                       )   
            setElementData( 
                v, 
                "Tempo jogado", 
                "h: "..string.format("%02d",tostring( hour ))..' m: '..string.format("%02d",tostring( min ))..' s: '..string.format("%02d",tostring( sec )) 
            ) 
        end 
    end, 
    1000, 0 
) 
    
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tempo jogado' ) 
        setAccountData ( playeraccount, "Tempo jogado", tostring(sValue) ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Tempo jogado" ) 
        if ( time ) then 
            setElementData ( source, "Tempo jogado", time ) 
                else 
            setElementData ( source, "Tempo jogado",0 ) 
        setAccountData ( playeraccount, "Tempo jogado",0 ) 
        end 
    end 
end 
  
  
  
function onPlayerLogOut ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tempo jogado' ) 
        setAccountData ( playeraccount, "Tempo jogado", tostring(sValue) ) 
    end 
    t[ source ] = nil 
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...