Jump to content

Counting time


Recommended Posts

The following code counts the player's playing time. I want it like this:

If the playing time is less than 1 hour then it should display in scoreboard the time in 'x minutes'.

If the playing time is more than an hour then it should display in scoreboard the time in 'x hours'.

If the playing time is more than 24 hours then it should display in scoreboard the time in 'x days'.

Days will be the maximum counting number.

Edited by Guest
Link to comment

Well, what I would do is store playtime in seconds and then count how many minutes, hours, days he plays. Like that

call ( getResourceFromName ( "scoreboard" ), "scoreboardAddColumn", "Play Time" ) ---Export of ScoreBoard 
  
addEventHandler("onPlayerLogin", root, function(_, acc) 
    if not isGuestAccount(acc) then 
        local playedTime = getAccountData(acc, "onlineTime"); 
        if not playedTime then 
            playedTime = 0; 
        end 
         
        setElementData(source, "onlineTime", playedTime); 
         
        local player = source; 
        local timer = setTimer(function() 
            setElementData(player, "onlineTime", getElementData(player, "onlineTime") + 1); 
            triggerEvent("onChangePlaytime", player); 
        end, 1000, 0); 
        setElementData(source, "timer:onlineTime", timer); 
         
    end 
end); 
  
addEventHandler("onChangePlaytime", root, function() 
    local playTime = getElementData(source, "onlineTime"); 
    if playTime then 
        if playTime >= 0 then 
            setElementData(player, "Play Time", playTime .. " seconds") 
        elseif playTime >= 60 then 
            playTime = math.floor(playTime / 60) 
            setElementData(player, "Play Time", playTime .. " minutes") 
        elseif playTime >= 3600 then 
            playTime = math.floor(playTime / 3600) 
            setElementData(player, "Play Time", playTime .. " hours") 
        elseif playTime >= 86400 then 
            playTime = math.floor(playTime / 86400) 
            setElementData(player, "Play Time", playTime .. " days") 
        end 
    end 
end); 
  
function saveData(player, acc) 
    if acc and not isGuestAccount(acc) then 
        setAccountData(acc, "onlineTime", getElementData(player, "onlineTime")); 
        if isTimer(getElementData(player, "timer:onlineTime")) then 
           killTimer(getElementData(player, "timer:onlineTime")); 
        end 
    end 
end 
  
addEventHandler("onPlayerDisconnect", root, function() 
    saveData(source, getPlayerAccount(source)); 
end); 
  
addEventHandler("onPlayerLogout", root, function(acc) 
   saveData(source, acc); 
end); 

Didn't test.

@EDIT: Updated.

Edited by Guest
Link to comment

It stores playtime in seconds, but shows it in format you wanted.

@EDIT: Found the error.

Replace last lines to this

function saveData(player, acc) 
    if acc and not isGuestAccount(acc) then 
        setAccountData(acc, "onlineTime", getElementData(player, "onlineTime")); 
        if isTimer(getElementData(player, "timer:onlineTime")) then 
           killTimer(getElementData(player, "timer:onlineTime")); 
        end 
    end 
end 
  
addEventHandler("onPlayerDisconnect", root, function() 
    saveData(source, getPlayerAccount(source)); 
end); 
  
addEventHandler("onPlayerLogout", root, function(acc) 
   saveData(source, acc); 
end); 

Link to comment

Works for me now. Changed it a bit

call ( getResourceFromName ( "scoreboard" ), "scoreboardAddColumn", "Play Time" ) ---Export of ScoreBoard 
  
function getOnlineTime(player, acc) 
    if not isGuestAccount(acc) then 
        local playedTime = getAccountData(acc, "onlineTime"); 
        if not playedTime then 
            playedTime = 0; 
        end 
        
        setElementData(player, "onlineTime", playedTime); 
        
        local timer = setTimer(function() 
            setElementData(player, "onlineTime", getElementData(player, "onlineTime") + 1); 
            updateScoreboard(player); 
        end, 60000, 0); 
        updateScoreboard(player); 
        setElementData(player, "timer:onlineTime", timer); 
    end 
end 
  
addEventHandler("onResourceStart", resourceRoot, function() 
    for _, player in ipairs(getElementsByType("player")) do 
        getOnlineTime(player, getPlayerAccount(player)); 
    end 
end); 
  
addEventHandler("onPlayerLogin", root, function(_, acc) 
    getOnlineTime(source, acc); 
end); 
  
function updateScoreboard(player) 
    local playTime = getElementData(player, "onlineTime"); 
    if playTime then 
        if playTime >= 0 and playTime < 60 then 
            setElementData(player, "Play Time", playTime .. " minutes"); 
        elseif playTime >= 60 and playTime < 1440 then 
            playTime = math.floor(playTime / 60) 
            setElementData(player, "Play Time", playTime .. " hours"); 
        elseif playTime >= 1440 then 
            playTime = math.floor(playTime / 1440) 
            setElementData(player, "Play Time", playTime .. " days"); 
        end 
    end 
end 
  
function saveData(player, acc) 
    if acc and not isGuestAccount(acc) then 
        setAccountData(acc, "onlineTime", getElementData(player, "onlineTime")); 
        if isTimer(getElementData(player, "timer:onlineTime")) then 
           killTimer(getElementData(player, "timer:onlineTime")); 
        end 
    end 
end 
  
addEventHandler("onPlayerDisconnect", root, function() 
    saveData(source, getPlayerAccount(source)); 
end); 
  
addEventHandler("onPlayerLogout", root, function(acc) 
   saveData(source, acc); 
end); 

Link to comment

onlineTime will not work since the scoreboard columns accept the element data which are named as the column, that means, it should be 'Play Time' instead of 'onlineTime'. I changed it but in both ways, it doesn't work.

Error at line 13, attempt to perform arithmetic on a string value.

Link to comment

This code works perfectly for me

call ( getResourceFromName ( "scoreboard" ), "scoreboardAddColumn", "Play Time" ) ---Export of ScoreBoard 
  
function getOnlineTime(player, acc) 
    if not isGuestAccount(acc) then 
        local playedTime = getAccountData(acc, "onlineTime"); 
        if not playedTime then 
            playedTime = 0; 
        end 
        
        setAccountData(acc, "onlineTime", playedTime); 
        setElementData(player, "onlineTime", playedTime); 
        
        local timer = setTimer(function() 
            setElementData(player, "onlineTime", getElementData(player, "onlineTime") + 1); 
            updateScoreboard(player); 
        end, 60000, 0); 
        updateScoreboard(player); 
        setElementData(player, "timer:onlineTime", timer); 
    end 
end 
  
addEventHandler("onResourceStart", resourceRoot, function() 
    for _, player in ipairs(getElementsByType("player")) do 
        getOnlineTime(player, getPlayerAccount(player)); 
    end 
end); 
  
addEventHandler("onPlayerLogin", root, function(_, acc) 
    getOnlineTime(source, acc); 
end); 
  
function updateScoreboard(player) 
    local playTime = getElementData(player, "onlineTime"); 
    if playTime then 
        if playTime >= 0 and playTime < 60 then 
            setElementData(player, "Play Time", playTime .. " minutes"); 
        elseif playTime >= 60 and playTime < 1440 then 
            playTime = math.floor(playTime / 60) 
            setElementData(player, "Play Time", playTime .. " hours"); 
        elseif playTime >= 1440 then 
            playTime = math.floor(playTime / 1440) 
            setElementData(player, "Play Time", playTime .. " days"); 
        end 
    end 
end 
  
function saveData(player, acc) 
    if acc and not isGuestAccount(acc) then 
        setAccountData(acc, "onlineTime", getElementData(player, "onlineTime")); 
        if isTimer(getElementData(player, "timer:onlineTime")) then 
           killTimer(getElementData(player, "timer:onlineTime")); 
        end 
    end 
end 
  
addEventHandler("onPlayerDisconnect", root, function() 
    saveData(source, getPlayerAccount(source)); 
end); 
  
addEventHandler("onPlayerLogout", root, function(acc) 
   saveData(source, acc); 
end); 

I have even created new account, just in case. Here are screens

https://scr.hu/jPm0A1

https://scr.hu/63rMWO

Link to comment

Another problem. The data isn't saving when the resource stops or the client logs out.

addEventHandler("onPlayerLogout", root, function(acc) 
   local timer = getElementData ( source , "timer:onlineTime" ) 
        if isTimer ( timer ) then 
            killTimer ( timer ) 
        end 
        setElementData(source, "Online Time", "0 minutes") 
        setAccountData(acc, "Online Time", getElementData(source, "Online Time")) 
    end) 
addEventHandler("onResourceStop", root, 
    function() 
    local players = getElementsByType("player") 
    for i,v in ipairs(players) do 
    setAccountData(getPlayerAccount(v), "Online Time", getElementData(v, "Online Time")) 
    end 
end)     

Link to comment

you know what, you are not requesting for help rather asking people to do edit the script, i have been seeing your posts since long. you are not even trying to do anything just posting this doesn't work etc. also whoamI don't post the full code every single time as it won't help him learn. no offense, you gotta learn.

Link to comment
I edited the code... I post the code after I have tried my best and when I am not able to understand what is causing the bugs. So when I get a reply, I understand how to fix it next time when I face the same problem.

dude you are clearly asking for help after each problem, and rather doing your own attempts WhoAmI is posting whole code.

Not fighting anymore :|

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