Jump to content

About setTimer


ViRuZGamiing

Recommended Posts

Posted

Greetings,

I'm trying to work with timers and I have some questions about which is the best way to complete this idea.

I have a timer that runs for 15 seconds and need to do something when that timer runs out,

Should I give my timer a name and create another timer which kills timer 1 after 15 seconds and do what has to be done?

If code is needed, I shall provide.

Regards Billy

Posted
function afterTimerTicks() 
    -- this is triggered after 15 seconds 
end 
setTimer(afterTimerTicks, 15000, 1) 

So what I said is indeed the best way of doing this?

My code in the spoiler (It is working fine just wondering if it's the best way of killing a timer after a time

function startPeeing () 
    sec = 15 
    minWidth = 0 
    count = 15 
    width = 790 
    increment = width / sec 
  
    addEventHandler("onClientRender", root, peeBarRender) 
     
    local peeTimer = setTimer( function () 
        width = width - increment 
        count = count - 1 
    end, 1000, sec) 
     
    setTimer ( function() 
        killTimer(peeTimer) 
        removeEventHandler("onClientRender", root, peeBarRender) 
    end, 15000, 1) 
end 

Regards Billy

Posted (edited)

You do not need to kill the timer. Once the timer finishes executing code it kills itself, that is, unless it is indefinite.

Edited by Guest
Posted

killTimer can be use in the following ways.

  
local x = 1 
function aTimer() 
    x = x+1 
end 
-- this is an infinite timer which runs every 10 seconds. 
myTimer = setTimer(aTimer, 10*1000, 0) 
  
-- kill the timer when x is 10 
if x = 10 then 
    killTimer(myTimer) 
end 
  
-- or kill the timer after 1 minute (after myTimer has ran 6 times). 
setTimer(killTimer, 60*1000, 1, myTimer) -- 
  

Posted
Okay and suppose the player leaves during a timer, I guess I'd have to kill it then as well?

Regards Billy

Only if it is executed by the server.

Posted
Okay and suppose the player leaves during a timer, I guess I'd have to kill it then as well?

Regards Billy

Only if it is executed by the server.

I am wondering, what would happen to a timer if it was executed by the client?

Posted (edited)
Army@1, it would terminate itself since the player has actually left the server.

I see. It would probably output an error but there would be no client to receive it, right?

Edited by Guest
Posted

It is not related in any way with the server. setTimer(..) on a client will set the timer locally, and when player leaves, all Lua execution terminates. It is different from the server Lua scripts since they run all the time (whenever the server is running, so server variable/process cleanup is recommended to lower RAM/processor usage)

Posted
It is not related in any way with the server. setTimer(..) on a client will set the timer locally, and when player leaves, all Lua execution terminates. It is different from the server Lua scripts since they run all the time (whenever the server is running, so server variable/process cleanup is recommended to lower RAM/processor usage)

Didn't know that. Thanks.

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