Jump to content

save time


BorderLine

Recommended Posts

Hi everyone,

i have a question and other is a help

first is how i can make to when pass 60 minuts, back to 0.

and the help.. my script dont save the time online

Im try much thinks, and dont work

debug dont show erros

 exports.BHZ4_scoreboard:addScoreboardColumn('Tiempo Jugado') 
  
setTimer( 
    function( ) 
         for _, v in pairs( getElementsByType( "player" ) ) do 
            local totalTime = getElementData( v,"totalTime" ) or 0 
            setElementData( v,"totalTime",totalTime+1 ) 
            setElementData( v,"Tiempo Jugado",math.floor( tonumber( totalTime )/3600 ).." H : "..math.floor( tonumber( totalTime )/60 ).." M" ) 
        end  
    end, 
1000, 
0 ) 
  
function onPlayerQuit ( ) 
       local playeraccount = getPlayerAccount ( source ) 
      if ( playeraccount ) and not isGuestAccount ( playeraccount ) then  
            local time = getAccountData ( playeraccount , totalTime )  
            setAccountData ( playeraccount, "totalTime", time ) 
      end 
end 
function onPlayerLogin (_, playeraccount )  
      if ( playeraccount ) then 
            local time = getAccountData ( playeraccount, "totalTime" ) 
           if ( time ) then 
                  setElementData ( playeraccount, time ) 
            end 
      end 
end 
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin) 
  

Link to comment
exports.BHZ4_scoreboard:addScoreboardColumn('Tiempo Jugado') 
  
setTimer( 
    function( ) 
         for _, v in pairs( getElementsByType( "player" ) ) do 
            local totalTime = getElementData( v,"totalTime" ) or 0 
            setElementData( v,"totalTime",totalTime+1 ) 
            setElementData( v,"Tiempo Jugado",math.floor( tonumber( totalTime )/3600 ).." H : "..math.floor( tonumber( totalTime )/60 ).." M" ) 
        end 
    end, 
    1000, 0  
) 
  
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local time = getElementData ( source, "totalTime" ) 
        setAccountData ( playeraccount, "totalTime", time ) 
    end 
end 
function onPlayerLogin (_, playeraccount ) 
      if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "totalTime" ) 
        if ( time ) then 
            setElementData ( source, "totalTime", time ) 
        end 
    end 
end 
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin) 

Link to comment
    exports.BHZ4_scoreboard:addScoreboardColumn('Tiempo Jugado') 
      
    setTimer( 
        function( ) 
             for _, v in pairs( getElementsByType( "player" ) ) do 
                local totalTime = getElementData( v,"totalTime" ) or 0 
                setElementData( v,"totalTime",totalTime+1 ) 
                setElementData( v,"Tiempo Jugado",math.floor( tonumber( totalTime )/3600 ).." H : "..math.floor( tonumber( totalTime )/59 ).." M" ) 
            end 
        end, 
        1000, 0 
    ) 
      
    function onPlayerQuit ( ) 
        local playeraccount = getPlayerAccount ( source ) 
        if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
            local time = getElementData ( source, "totalTime" ) 
            setAccountData ( playeraccount, "totalTime", time ) 
        end 
    end 
    function onPlayerLogin (_, playeraccount ) 
          if ( playeraccount ) then 
            local time = getAccountData ( playeraccount, "totalTime" ) 
            if ( time ) then 
                setElementData ( source, "totalTime", time ) 
            end 
        end 
    end 
    addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) 
    addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin) 

?

Link to comment
exports.BHZ4_scoreboard:addScoreboardColumn('Tiempo Jugado') 
  
local t = { } 
  
function table.empty( t ) 
    if type( t ) ~= "table" then 
        return false 
    end 
     
    return not next( t ) 
end 
  
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 
    return arg1,arg2 
end 
      
setTimer( 
    function( ) 
        for _, v in pairs( getElementsByType( "player" ) ) do 
            if not t[ v ] then 
                t[ v ] = v 
            end 
            if table.empty( t[ v ] ) then 
                t[ v ] = { } 
            end 
            t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1 
            local min,hour = checkValues ( 
                    v, 
                    t[ v ][ 'min' ] or 0, 
                    t[ v ][ 'hour' ] or 0 
            )    
            setElementData(  
                v, 
                "Tiempo Jugado", 
                tostring( hour )..':'..tostring( min ) 
            ) 
        end 
    end, 
    1000, 0 
) 
     
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tiempo Jugado' ) 
        setAccountData ( playeraccount, "Tiempo Jugado", sValue ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Tiempo Jugado" ) 
        if ( time ) then 
            setElementData ( source, "Tiempo Jugado", time ) 
        end 
    end 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 

Updated.

Link to comment

Try this:

local t = { } 
  
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 
    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,hour = checkValues ( 
                    v, 
                    t[ v ][ 'min' ] or 0, 
                    t[ v ][ 'hour' ] or 0 
            )    
            setElementData( 
                v, 
                "Tiempo Jugado", 
                tostring( hour )..':'..tostring( min ) 
            ) 
        end 
    end, 
    1000, 0 
) 
    
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tiempo Jugado' ) 
        setAccountData ( playeraccount, "Tiempo Jugado", sValue ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Tiempo Jugado" ) 
        if ( time ) then 
            setElementData ( source, "Tiempo Jugado", time ) 
        end 
    end 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 

Link to comment
local t = { } 
  
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 
      
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 
            checkValues ( 
                v, 
                t[ v ][ 'sec' ] or 0, 
                t[ v ][ 'min' ] or 0 
            )    
            setElementData( 
                v, 
                "Tiempo Jugado", 
                tostring( t[ v ][ 'hour' ] or 0 )..':'..tostring( t[ v ][ 'min' ] or 0 ) 
            ) 
        end 
    end, 
    1000, 0 
) 
    
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tiempo Jugado' ) 
        setAccountData ( playeraccount, "Tiempo Jugado", sValue ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Tiempo Jugado" ) 
        if ( time ) then 
            setElementData ( source, "Tiempo Jugado", time ) 
        end 
    end 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 

Again updated.

Link to comment

Try

local t = { } 
  
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 
      
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 
            checkValues ( 
                v, 
                t[ v ][ 'sec' ] or 0, 
                t[ v ][ 'min' ] or 0 
            )    
            setElementData( 
                v, 
                "Tiempo Jugado", 
                tostring( t[ v ][ 'hour' ] or 0 )..':'..tostring( t[ v ][ 'min' ] or 0 ) 
            ) 
        end 
    end, 
    1000, 0 
) 
    
function onPlayerQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
        local sValue = getElementData( source,'Tiempo Jugado' ) 
        setAccountData ( playeraccount, "Tiempo Jugado", tostring( sValue ) ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "Tiempo Jugado" ) 
        if ( time ) then 
            setElementData ( source, "Tiempo Jugado", tostring( time ) )     
        end 
    end 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 

I tested it later.

Link to comment

Not work, i think the problem is in

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 
            checkValues ( 
                v, 
                t[ v ][ 'sec' ] or 0, 
                t[ v ][ 'min' ] or 0 
            )   
            setElementData( 
                v, 
                "Tiempo Jugado", 
                tostring( t[ v ][ 'hour' ] or 0 )..':'..tostring( t[ v ][ 'min' ] or 0 ) 
            ) 
        end 
    end, 
    1000, 0 
) 
  

For example, im stay 5 minutes in the server, then i quit and i login,and when i login the time in score show 0:5 minutes. but one second after come back the time to 0:0

Link to comment
exports.scoreboard:addScoreboardColumn( 'Tiempo Jugado' ) 
  
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,'Tiempo Jugado' ) 
        setAccountData ( account, "Tiempo Jugado", 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,'Tiempo Jugado' ) 
        setAccountData ( playeraccount, "Tiempo Jugado", 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, "Tiempo Jugado" ) 
                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, 
                    "Tiempo Jugado",  
                    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 ) 

Again updated.

Link to comment
  • 3 months later...
hi dude can i use this code ?

try this dude !!.

  
  
exports.scoreboard:addScoreboardColumn('PlayTime') 
  
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, 
                "PlayTime", 
                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,'PlayTime' ) 
        setAccountData ( playeraccount, "PlayTime", tostring(sValue) ) 
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "PlayTime" ) 
        if ( time ) then 
            setElementData ( source, "PlayTime", time ) 
                else 
            setElementData ( source, "PlayTime",0 ) 
        setAccountData ( playeraccount, "PlayTime",0 ) 
        end 
    end 
end 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 
  
  
  
  

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