Jump to content

Play Time Help


mint3d

Recommended Posts

Ok so I got this code it's either not saving the playtime or its not loading it when a player joins please help me out..

exports.scoreboard:addScoreboardColumn('Online Time') 
  
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

Didn't work at all got errors saying

line 25 Bad argument @ 'addEventHandler' [Expected function at argument 3, got nil]

line 26 Bad argument @ 'addEventHandler' [Expected function at argument 3, got nil]

line 56 Bad argument @ 'addEventHandler' [Expected function at argument 3, got nil]

line 57 Bad argument @ 'addEventHandler' [Expected function at argument 3, got nil]

line 68 Bad argument @ 'addEventHandler' [Expected function at argument 3, got nil]

Link to comment

Here is full script enjoy PLAYING MTA :D

Client Side

  
--Update Scoreboard stuff 
function updateScoreboardSettings() 
   
    local playTime = getElementData(localPlayer, "playTime") 
   
    setElementData(localPlayer, "Play Time", playTime) 
  
end 
setTimer(updateScoreboardSettings, 500, 0) 
  

Server

  
function loadPlayTimeAll() 
    for index, player in pairs(getElementsByType("player")) do 
        if (isGuestAccount(getPlayerAccount(player))) then 
            local account = getPlayerAccount(player) 
            local minutes = getAccountData(account, "playTime") 
            if (tonumber(minutes)) then 
                minutes = tonumber(minutes) 
                local hours = math.floor(minutes / 60) 
                if (hours > 0) then 
                    setElementData(player, "playTime", hours.." Hours") 
                else 
                    setElementData(player, "playTime", minutes.." Minutes") 
                end 
            else 
                setAccountData(account, "playTime", 0) 
                setElementData(player, "playTime", "0 Minutes") 
            end 
            playTimeTimer[player] = setTimer(incrementPlayTime, 60000, 0, player) 
        end 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, loadPlayTimeAll) 
  
--Load on Play time on player login 
function loadPlayTime(account) 
    local minutes = getAccountData(account, "playTime") 
    if (tonumber(minutes)) then 
        minutes = tonumber(minutes) 
        local hours = math.floor(minutes / 60) 
        if (hours > 0) then 
            setElementData(source, "playTime", hours.." Hours") 
        else 
            setElementData(source, "playTime", minutes.." Minutes") 
        end 
    else 
        setAccountData(account, "playTime", 0) 
        setElementData(source, "playTime", "0 Minutes") 
    end 
    playTimeTimer[source] = setTimer(incrementPlayTime, 60000, 0, source) 
end 
addEventHandler("onPlayerLogin", root, loadPlayTime) 
  
function stopPlayTime() 
    if (isTimer(playTimeTimer[source])) then killTimer(playTimeTimer[source]) end 
end 
addEventHandler("onPlayerLogout", root, stopPlayTime) 
  
function incrementPlayTime(player) 
    if (not player or not isElement(player)) then return end 
    if (isGuestAccount(getPlayerAccount(player))) then return end 
    local account = getPlayerAccount(player) 
    local minutes = getAccountData(account, "playTime") or 1 
    minutes = tonumber(minutes) 
    minutes = minutes + 1 
    local hours = math.floor(minutes / 60) 
    if (hours > 0) then 
        setElementData(player, "playTime", hours.." Hours") 
    else 
        setElementData(player, "playTime", minutes.." Minutes") 
    end 
    setAccountData(account, "playTime", minutes) 
end 
  
function getPlayerPlayTime(player) 
    if (isGuestAccount(getPlayerAccount(player))) then return false end 
    local account = getPlayerAccount(player) 
    local minutes = getAccountData(account, "playTime") 
    return tonumber(minutes) 
end 
  
  

I think it will work fine i didnt tested it yet. If any bug post it.

Link to comment

It can just be a server sided function :lol:

Heres the code :

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' ) 
    local hour = tonumber( t[ source ][ 'hour' ] or 0 ) 
    local min = tonumber( t[ source ][ 'min' ] or 0 ) 
    local sec = tonumber( t[ source ][ 'sec' ] or 0 ) 
        setAccountData ( playeraccount, "PlayTime-hour", tostring(hour) ) 
        setAccountData ( playeraccount, "PlayTime-min", tostring(min) ) 
        setAccountData ( playeraccount, "PlayTime-sec", tostring(sec) ) 
        setAccountData ( playeraccount, "PlayTime", tostring(sValue) ) 
  
    end 
    t[ source ] = nil 
end 
  
function onPlayerLogin (_, playeraccount ) 
    if ( playeraccount ) then 
        local time = getAccountData ( playeraccount, "PlayTime" ) 
    local hou = getAccountData ( playeraccount, "PlayTime-hour") 
    local min = getAccountData ( playeraccount, "PlayTime-min") 
    local sec = getAccountData ( playeraccount, "PlayTime-sec") 
        if ( time ) then 
            setElementData ( source, "PlayTime", time ) 
  
                             t[ source ]["hour"] = tonumber(hou) 
                             t[ source ]["min"] = tonumber(min) 
                             t[ source ]["sec"] = tonumber(sec) 
                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...