FlyingSpoon Posted February 24, 2016 Share Posted February 24, 2016 How can I create a 2 minute timer, using getTickCount() on DX Text? Someone help please! Link to comment
KariiiM Posted February 24, 2016 Share Posted February 24, 2016 Try something by yourself and post what you have tried, and I will help you Link to comment
FlyingSpoon Posted February 24, 2016 Author Share Posted February 24, 2016 screenX,screenY = guiGetScreenSize() function startTheClock () systemUpTime = 2000 --Store the system tick count, this will be 0 for us currentCount = getTickCount () dxDrawRectangle (screenX *.40, screenY * .09, 250, 50, tocolor(0,0,0,150)) dxDrawText ( currentCount - systemUpTime, screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) end addEventHandler ( "onClientRender", root, startTheClock ) Link to comment
KariiiM Posted February 24, 2016 Share Posted February 24, 2016 Try that and tell me the result, screenX,screenY = guiGetScreenSize() function startTheClock () systemUpTime = 120000 currentCount = getTickCount () dxDrawRectangle (screenX *.40, screenY * .09, 250, 50, tocolor(0,0,0,150)) dxDrawText(tostring(currentCount-systemUpTime+getTickCount()), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) end addEventHandler ( "onClientRender", root, startTheClock) btw, 2000 not equal 2minutes, it's 2secs ! Link to comment
Bonus Posted February 24, 2016 Share Posted February 24, 2016 KariiiM, your code will draw something like 199999, always That is a way: local starttime = 0 local screenX,screenY = guiGetScreenSize() local function startTheClock ( ) if getTickCount()-starttime >= 2 * 60 * 1000 then -- if 2 min past -- whatever you want to do else -- whatever you want to do end end addCommandHandler ( "starttheclock", function ( ) starttime = getTickCount() addEventHandler ( "onClientRender", root, startTheClock ) end ) Link to comment
KariiiM Posted February 24, 2016 Share Posted February 24, 2016 KariiiM, your code will draw something like 199999, always He can deal with that by using math.floor Link to comment
Bonus Posted February 24, 2016 Share Posted February 24, 2016 Your code: local currentCount = getTickCount() currentCount - systemUpTime + getTickCount() What it does: getTickCount() some ms ago - 120000 + getTickCount() now example: 199 - 120000 + 200 It will always be something like -199999. Link to comment
KariiiM Posted February 24, 2016 Share Posted February 24, 2016 You are right @Bonus I just tested my code now Link to comment
FlyingSpoon Posted February 24, 2016 Author Share Posted February 24, 2016 Bonus, how can I create a timer using your script to do a countdown, so the player can see how long is left. Link to comment
Bonus Posted February 24, 2016 Share Posted February 24, 2016 local endtime = 0 local function renderCountdown ( ) local lefttime = endtime - getTickCount() if lefttime > 0 then dxDrawText ( math.ceil ( lefttime ) ... elseif lefttime >= -3000 then dxDrawText ( "GO!" ... ) else removeEventHandler ( "onClientRender", root, renderCountdown ) end end addCommandHandler ( "startcountdown", function ( _, timeinsec ) endtime = getTickCount() + timeinsec * 1000 -- To save the time when the countdown will end addEventHandler ( "onClientRender", root, renderCountdown ) end ) Link to comment
FlyingSpoon Posted February 25, 2016 Author Share Posted February 25, 2016 local endtime = 0 local function renderCountdown ( ) local lefttime = endtime - getTickCount() if lefttime > 0 then dxDrawText(math.ceil ( lefttime ), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) elseif lefttime >= -3000 then dxDrawText("GO!", screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) else removeEventHandler ( "onClientRender", root, renderCountdown ) end end addCommandHandler ( "test", function ( _, timeinsec ) endtime = getTickCount() + timeinsec * 1000 -- To save the time when the countdown will end addEventHandler ( "onClientRender", root, renderCountdown ) end ) Doesn't display my time left. Link to comment
Bonus Posted February 25, 2016 Share Posted February 25, 2016 Because you can't use "test" in an addCommandHandler ... "Note: You cannot use "list" or "test" as command name." Link to comment
FlyingSpoon Posted February 25, 2016 Author Share Posted February 25, 2016 local endtime = 0 local function renderCountdown ( ) local lefttime = endtime - getTickCount() if lefttime > 0 then dxDrawText(math.ceil ( lefttime ), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) elseif lefttime >= -3000 then dxDrawText("GO!", screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) else removeEventHandler ( "onClientRender", root, renderCountdown ) end end addCommandHandler ( "timer", function ( _, timeinsec ) endtime = getTickCount() + timeinsec * 1000 -- To save the time when the countdown will end addEventHandler ( "onClientRender", root, renderCountdown ) end ) attempt to perform arithmetic on local 'timeinsec' (a nil value) Link to comment
Bonus Posted February 25, 2016 Share Posted February 25, 2016 Use /timer 5 I expected you to know how to use commands. Oh and use tonumber ( timeinsec ) there endtime = getTickCount() + tonumber ( timeinsec ) * 1000 Link to comment
FlyingSpoon Posted February 26, 2016 Author Share Posted February 26, 2016 How can I now convert this into Minutes only, I know it's / 1000 but how would I go about doing it. Like countdown: 2:00 .. 1:59 .. 1:58 etc Link to comment
Bonus Posted February 26, 2016 Share Posted February 26, 2016 local totalleft = endtime - getTickCount() local minutesleft = math.floor ( totalleft / 60000 ) local secondsleft = math.floor ( ( totalleft - minutsleft * 60000 ) / 1000 ) local time = minutesleft..":"..secondsleft >= 10 and secondsleft or "0"..secondsleft time is the converted time. Should work, test it. Link to comment
FlyingSpoon Posted February 26, 2016 Author Share Posted February 26, 2016 screenX,screenY = guiGetScreenSize() local function renderCountdown ( ) local lefttime = endtime - getTickCount() local minutesleft = math.floor ( lefttime / 60000 ) local secondsleft = math.floor ( ( lefttime - minutesleft * 60000 ) / 1000 ) local timet = minutesleft..":"..secondsleft >= 10 and secondsleft or "0"..secondsleft if lefttime > 0 then dxDrawText(math.ceil ( timet ), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) elseif lefttime >= -3000 then dxDrawText("GO!", screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) else removeEventHandler ( "onClientRender", root, renderCountdown ) end end addCommandHandler ( "timer", function ( _, timeinsec ) endtime = getTickCount() + tonumber ( timeinsec ) * 1000 addEventHandler ( "onClientRender", root, renderCountdown ) end ) I did this but it don't work. Link to comment
Bonus Posted February 26, 2016 Share Posted February 26, 2016 math.ceil with a string? Link to comment
FlyingSpoon Posted February 27, 2016 Author Share Posted February 27, 2016 What do you mean? Link to comment
Bonus Posted February 27, 2016 Share Posted February 27, 2016 math.ceil with a string doesnt work Link to comment
FlyingSpoon Posted February 27, 2016 Author Share Posted February 27, 2016 Should I do 'lefftime' in there then? Link to comment
Bonus Posted February 27, 2016 Share Posted February 27, 2016 ... Just don't use math.ceil ... You want to draw the time like that "5:31", so why would you want to use timeleft? 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