Wisin Posted July 27, 2010 Share Posted July 27, 2010 hi, i would like to save how much a player played in the server, but i dont know what functions to use etc... Link to comment
50p Posted July 27, 2010 Share Posted July 27, 2010 https://wiki.multitheftauto.com/wiki/GetTickCount - when he joins and when he leaves. You even have an example of how it'd work on that page. Link to comment
Wisin Posted July 27, 2010 Author Share Posted July 27, 2010 ive done this, client: localPlayer = getLocalPlayer() function addColumns() exports.scoreboard:scoreboardAddColumn("joinTime") end addEventHandler("onClientResourceStart",getResourceRootElement(),addColumns) function get() local timeOnline = (getTickCount() - getElementData ( getLocalPlayer(), "joinTime" )) / 1000 setElementData(getLocalPlayer(),"joinTime",timeOnline) end addEventHandler( "onClientRender", getRootElement(), get) server: function joinTime ( ) local getTickStart = getTickCount () setElementData ( source, "joinTime", getTickStart ) end addEventHandler ( "onPlayerJoin", getRootElement(), joinTime ) but it sets a big number not seconds/minutes like i want... Link to comment
dzek (varez) Posted July 27, 2010 Share Posted July 27, 2010 do some maths to convert it to hours/minutes/seconds Link to comment
Wisin Posted July 27, 2010 Author Share Posted July 27, 2010 do some maths to convert it to hours/minutes/seconds i dont know how to do this... Link to comment
Dark Dragon Posted July 27, 2010 Share Posted July 27, 2010 milliseconds / 1000 = seconds (1 second has 1000 milliseconds) milliseconds /60000 = minutes (1 minute has 60 seconds) etc. Link to comment
dzek (varez) Posted July 27, 2010 Share Posted July 27, 2010 think take pen and paper and try the thing you have to know the most when programming is how to think if you dont want to think - dont try to write scripts example of time converting: take any seconds count divite it by 3600 (60*60 - minutes * seconds) to get how many hours is in that seconds count round down (floor) this value ---- THIS IS HOUR COUNT then multiple this value by 3600 now subtract from 12430 your multiplied value --- lets say its "X" now now divide it by 60 to get minutes round down (floor) this value ---- THIS IS YOUR MINUTES COUNT multiple floored value by 60 from "X" subtract your last value --- THIS IS YOUR SECONDS COUNT simple math looks complcated on forums, easy on script.. and addional help: function doing what you need but written in PYTHON not lua. but its EXTREMLY EASY to convert it.. just 1 minute of thinking.. rly def convert_to_human_time(dana): dana=int(dana) hours = math.floor(dana/3600) minutes = math.floor((dana-hours*3600)/60) seconds = math.floor(dana-hours*3600-minutes*60) retStr = str(int(hours))+"h "+str(int(minutes))+"min "+str(int(seconds))+"s" return retStr - def is the function definition, rest is the function body.. - use tonumber instead of int - in lua you are joining strings not with + but with ".." (two dots) - you dont need that int/tonumber in retStr line in lua Link to comment
50p Posted July 27, 2010 Share Posted July 27, 2010 Or take a look into Race resource... It must have such function. Link to comment
[DMC] Posted July 28, 2010 Share Posted July 28, 2010 the race resource starts the timer on spawn? that can help with this Link to comment
Wisin Posted July 28, 2010 Author Share Posted July 28, 2010 thinktake pen and paper and try the thing you have to know the most when programming is how to think if you dont want to think - dont try to write scripts example of time converting: take any seconds count divite it by 3600 (60*60 - minutes * seconds) to get how many hours is in that seconds count round down (floor) this value ---- THIS IS HOUR COUNT then multiple this value by 3600 now subtract from 12430 your multiplied value --- lets say its "X" now now divide it by 60 to get minutes round down (floor) this value ---- THIS IS YOUR MINUTES COUNT multiple floored value by 60 from "X" subtract your last value --- THIS IS YOUR SECONDS COUNT simple math looks complcated on forums, easy on script.. and addional help: function doing what you need but written in PYTHON not lua. but its EXTREMLY EASY to convert it.. just 1 minute of thinking.. rly def convert_to_human_time(dana): dana=int(dana) hours = math.floor(dana/3600) minutes = math.floor((dana-hours*3600)/60) seconds = math.floor(dana-hours*3600-minutes*60) retStr = str(int(hours))+"h "+str(int(minutes))+"min "+str(int(seconds))+"s" return retStr - def is the function definition, rest is the function body.. - use tonumber instead of int - in lua you are joining strings not with + but with ".." (two dots) - you dont need that int/tonumber in retStr line in lua ive "converted" it, function convert_to_human_time(time) time=tonumber(time) hours = math.floor(time/3600) minutes = math.floor((time-hours*3600)/60) seconds = math.floor(time-hours*3600-minutes*60) retStr = str(tonumber(hours)).."h "..str(tonumber(minutes)).."min "..str(tonumber(seconds)).."s" outputChatBox( "Time: " .. retStr .. "!", getRootElement(), 0, 255, 0 ) return retStr end addCommandHandler( "test",convert_to_human_time) says a nil value at "time" Link to comment
dzek (varez) Posted July 28, 2010 Share Posted July 28, 2010 function convert_to_human_time(time) time=tonumber(time) hours = math.floor(time/3600) minutes = math.floor((time-hours*3600)/60) seconds = math.floor(time-hours*3600-minutes*60) retStr = str(tonumber(hours)).."h "..str(tonumber(minutes)).."min "..str(tonumber(seconds)).."s" outputChatBox( "Time: " .. retStr .. "!", getRootElement(), 0, 255, 0 ) return retStr end addCommandHandler( "test",convert_to_human_time) says a nil value at "time" i forgot to tell you to replace "str" with "tostring" .. but you could have done it by yourself with a little search.. btw: better dont use "time" as variable name.. it should work but i dont recommend it Link to comment
Wisin Posted July 28, 2010 Author Share Posted July 28, 2010 function convert_to_human_time(time) time=tonumber(time) hours = math.floor(time/3600) minutes = math.floor((time-hours*3600)/60) seconds = math.floor(time-hours*3600-minutes*60) retStr = str(tonumber(hours)).."h "..str(tonumber(minutes)).."min "..str(tonumber(seconds)).."s" outputChatBox( "Time: " .. retStr .. "!", getRootElement(), 0, 255, 0 ) return retStr end addCommandHandler( "test",convert_to_human_time) says a nil value at "time" i forgot to tell you to replace "str" with "tostring" .. but you could have done it by yourself with a little search.. btw: better dont use "time" as variable name.. it should work but i dont recommend it it keeps saying attempt to perform airthmetic on local "dana" (a nil value) function convert_to_human_time(dana) dana=tonumber(dana) hours = math.floor(dana/3600) minutes = math.floor((dana-hours*3600)/60) seconds = math.floor(dana-hours*3600-minutes*60) retStr = tostring(tonumber(hours)).."h "..tostring(tonumber(minutes)).."min "..tostring(tonumber(seconds)).."s" outputChatBox( "Time: " .. retStr .. "!", getRootElement(), 0, 255, 0 ) return retStr end addCommandHandler( "test",convert_to_human_time) Link to comment
dzek (varez) Posted July 28, 2010 Share Posted July 28, 2010 are you sure you are passing value to convert? i mean, for example: /test 1234235 it will be nil if you are put just /test Link to comment
Wisin Posted July 28, 2010 Author Share Posted July 28, 2010 are you sure you are passing value to convert?i mean, for example: /test 1234235 it will be nil if you are put just /test yes, i do that and still same problem... Link to comment
dzek (varez) Posted July 28, 2010 Share Posted July 28, 2010 ill check it as i cannot spot error in browser edit: oh, addCommandHandler on server side it will be function convert_to_human_time(thePlayer, commandName, dana) on client: function convert_to_human_time(commandName, dana) this is basics.. you really have to spend some time on learning.. Link to comment
Wisin Posted July 28, 2010 Author Share Posted July 28, 2010 ill check it as i cannot spot error in browseredit: oh, addCommandHandler on server side it will be function convert_to_human_time(thePlayer, commandName, dana) on client: function convert_to_human_time(commandName, dana) this is basics.. you really have to spend some time on learning.. ah i forgot about that but how i can make it now it will put the online time at scoreboard? Link to comment
dzek (varez) Posted July 28, 2010 Share Posted July 28, 2010 you put it already, afair.. ?? just convert it with this before assigning element data.. Link to comment
Wisin Posted July 29, 2010 Author Share Posted July 29, 2010 you put it already, afair..?? just convert it with this before assigning element data.. i tryed this, function onlineTime() local getTickStart = getTickCount () dana=tonumber(getTickStart) hours = math.floor(dana/3600) minutes = math.floor((dana-hours*3600)/60) seconds = math.floor(dana-hours*3600-minutes*60) retStr = tostring(tonumber(hours)).."h "..tostring(tonumber(minutes)).."min "..tostring(tonumber(seconds)).."s" setElementData(source,"joinTime",retStr) return retStr end function getTickTimeStr() return onlineTime(getTickCount()) end addEventHandler( "onResourceStart",getResourceRootElement(getThisResource()),getTickTimeStr) Link to comment
dzek (varez) Posted July 29, 2010 Share Posted July 29, 2010 you wanted to show play time not join time, right? on every player join (btw, do it server side) - save his join time in table/array: joinTimes = { } function onJoin() joinTimes[source]=getTickCount() end then every second or somthing loop through all players (getElementsByType) for each of them get actual tick count, subtract actual from join, convert it with function, and set element data Link to comment
50p Posted July 29, 2010 Share Posted July 29, 2010 you wanted to show play time not join time, right?on every player join (btw, do it server side) - save his join time in table/array: joinTimes = { } function onJoin() joinTimes[source]=getTickCount() end then every second or somthing loop through all players (getElementsByType) for each of them get actual tick count, subtract actual from join, convert it with function, and set element data Why getElementsByType if you have a table of players already? getElementsByType is not as good as you may think it is. It's a slow function and if you have a table of players then use it instead. Link to comment
Wisin Posted July 29, 2010 Author Share Posted July 29, 2010 ive done this with a timer and does a weird thing, joinTimes = { } function onJoin() joinTimes[source]=getTickCount() end addEventHandler("onPlayerJoin",getRootElement(),onJoin) function onlineTime(playerSource) local getTickStart = getTickCount () dana=tonumber(getTickStart) hours = math.floor(dana/3600) minutes = math.floor((dana-hours*3600)/60) seconds = math.floor(dana-hours*3600-minutes*60) retStr = tostring(tonumber(hours)).."h "..tostring(tonumber(minutes)).."min "..tostring(tonumber(seconds)).."s" setElementData(playerSource,"joinTime",retStr) return retStr end function timer(playerSource) setTimer(onlineTime, 50, 0, playerSource) end addCommandHandler ("m", timer) this is what it does: 1394H 20MINS 50SECS and starts to do lot and lots of hours... Link to comment
dzek (varez) Posted July 29, 2010 Share Posted July 29, 2010 well, your right 50p. Wisin, you dont understand your code right? you should get back to basics Link to comment
[DMC] Posted July 31, 2010 Share Posted July 31, 2010 looks like hes not a bad script, he made a code 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