Jump to content

Help Time


K4stic

Recommended Posts

i Find this script on Community and i need Help to remake it

The Problem is then i restart the resource and i'm in Game then it set me 0 hours/min/sec

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 )..' Hours') 
        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 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
  
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 ( "onPlayerLogin", root, onPlayerLogin ) 
addEventHandler ( "onResourceStart", resourceRoot, onPlayerLogin ) 

Link to comment
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 )..' Hours') 
        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 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
  
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 ) 
        end 
    end 
end 
addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) 
addEventHandler ( "onResourceStart", resourceRoot, onPlayerLogin ) 
  
function onrestart ( ) 
    local time = getAccountData ( playeraccount, "PlayTime" ) 
    local hou = getAccountData ( playeraccount, "PlayTime-hour") 
    local min = getAccountData ( playeraccount, "PlayTime-min") 
    local sec = getAccountData ( playeraccount, "PlayTime-sec") 
        setElementData ( source, "PlayTime", time ) 
        t[ source ]["hour"] = tonumber(hou) or 0 
        t[ source ]["min"] = tonumber(min) or 0 
        t[ source ]["sec"] = tonumber(sec) or 0 
    end 
end 
addEventHandler ( "onResourceStart", resourceRoot, onrestart ) 

something like this?

Link to comment

Errors :

- playeraccount is not defined .

- The source of 'onResourceStart' is the root element in the resource that started, Not a player element

You should do a 'for' loop, Get all players, Check there accounts' data, And then set there data if it was found .

Link to comment

so use this?

function onrestart ( playeraccount ) 
    local time = getAccountData ( playeraccount, "PlayTime" ) 
    local hou = getAccountData ( playeraccount, "PlayTime-hour") 
    local min = getAccountData ( playeraccount, "PlayTime-min") 
    local sec = getAccountData ( playeraccount, "PlayTime-sec") 
    for _,player in ipairs ( getElementsByType ( "player" ) ) do 
        setElementData ( source, "PlayTime", time ) 
        t[ player ]["hour"] = tonumber(hou) or 0 
        t[ player ]["min"] = tonumber(min) or 0 
        t[ player ]["sec"] = tonumber(sec) or 0 
    end 
end 
addEventHandler ( "onResourceStart", resourceRoot, onrestart ) 

Link to comment
function onrestart (  ) 
  for _,player in ipairs ( getElementsByType ( "player" ) ) do 
     local playeraccount = getPlayerAccount ( player )  
     if not playeraccount then return end 
    local time = getAccountData ( playeraccount, "PlayTime" ) 
    local hou = getAccountData ( playeraccount, "PlayTime-hour") 
    local min = getAccountData ( playeraccount, "PlayTime-min") 
    local sec = getAccountData ( playeraccount, "PlayTime-sec") 
        setElementData ( player, "PlayTime", time ) 
        t[ player ]["hour"] = tonumber(hou) or 0 
        t[ player ]["min"] = tonumber(min) or 0 
        t[ player ]["sec"] = tonumber(sec) or 0 
    end 
end 
addEventHandler ( "onResourceStart", resourceRoot, onrestart ) 

Link to comment
function onrestart (  ) 
    for _,player in ipairs ( getElementsByType ( "player" ) ) do 
        local playeraccount = getPlayerAccount ( player ) 
        if isGuestAccount ( playeraccount ) then return end 
        local time = getAccountData ( playeraccount, "PlayTime" ) 
        local hou = getAccountData ( playeraccount, "PlayTime-hour") 
        local min = getAccountData ( playeraccount, "PlayTime-min") 
        local sec = getAccountData ( playeraccount, "PlayTime-sec") 
        setElementData ( player, "PlayTime", time ) 
        t[ player ]["hour"] = tonumber(hou) or 0 
        t[ player ]["min"] = tonumber(min) or 0 
        t[ player ]["sec"] = tonumber(sec) or 0 
    end 
end 
addEventHandler ( "onResourceStart", resourceRoot, onrestart ) 

@ #X_Mr.Pres[T]ege_X# :

The players will allways have an account ( a guest account ) .

Link to comment
function onrestart (  ) 
    for _,player in ipairs ( getElementsByType ( "player" ) ) do 
        local playeraccount = getPlayerAccount ( player ) 
        if isGuestAccount ( playeraccount ) then return end 
        local time = getAccountData ( playeraccount, "PlayTime" ) 
        local hou = getAccountData ( playeraccount, "PlayTime-hour") 
        local min = getAccountData ( playeraccount, "PlayTime-min") 
        local sec = getAccountData ( playeraccount, "PlayTime-sec") 
        setElementData ( player, "PlayTime", time ) 
        t[ player ]["hour"] = tonumber(hou) or 0 
        t[ player ]["min"] = tonumber(min) or 0 
        t[ player ]["sec"] = tonumber(sec) or 0 
    end 
end 
addEventHandler ( "onResourceStart", resourceRoot, onrestart ) 

@ #X_Mr.Pres[T]ege_X# :

The players will allways have an account ( a guest account ) .

HeHeHe what's the problem ? The script work with a guest account :lol: !

Link to comment
function onrestart (  ) 
  for _,player in ipairs ( getElementsByType ( "player" ) ) do 
     local playeraccount = getPlayerAccount ( player )  
    if ( playeraccount ) and not isGuestAccount ( 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") 
        setElementData ( player, "PlayTime", time ) 
  t[ player ] = {} 
        t[ player ]["hour"] = tonumber(hou) or 0 
        t[ player ]["min"] = tonumber(min) or 0 
        t[ player ]["sec"] = tonumber(sec) or 0 
  end 
 end 
end 
addEventHandler ( "onResourceStart", resourceRoot, onrestart ) 

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