Jump to content

time


Wisin

Recommended Posts

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

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

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

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

ah i forgot about that -_-

but how i can make it now it will put the online time at scoreboard?

Link to comment
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

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

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

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