Gravestone Posted June 1, 2016 Share Posted June 1, 2016 (edited) 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 June 11, 2016 by Guest Link to comment
WhoAmI Posted June 1, 2016 Share Posted June 1, 2016 (edited) 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 June 2, 2016 by Guest Link to comment
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 This increases the playtime in seconds only. Link to comment
WhoAmI Posted June 2, 2016 Share Posted June 2, 2016 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
WhoAmI Posted June 2, 2016 Share Posted June 2, 2016 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
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 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
WhoAmI Posted June 2, 2016 Share Posted June 2, 2016 What the... man... Look updateScoreboard function. Everything works fine. I've tested it properly. Link to comment
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 Are you sure? I created a new account just incase the data might be bugged, but the problem is the same. This appears after every minute in the debugscript Error at line 13, attempt to perform arithmetic on a string value. Link to comment
WhoAmI Posted June 2, 2016 Share Posted June 2, 2016 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
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 Alright its working now. Thanks. Link to comment
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 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
WhoAmI Posted June 2, 2016 Share Posted June 2, 2016 Because you are setting online time to 0 setElementData(source, "Online Time", "0 minutes") Link to comment
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 Well it should save the data as well as set it to 0 since the account the player was using isn't being used. Link to comment
Anubhav Posted June 2, 2016 Share Posted June 2, 2016 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
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 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. Link to comment
Anubhav Posted June 2, 2016 Share Posted June 2, 2016 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
Gravestone Posted June 2, 2016 Author Share Posted June 2, 2016 The data is liked saved. When I logout, my time is set to 47 minutes and when I restart the resource, my time is set to 47 minutes. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now