Do a function only once

For example:

[lua]
local num=1
local function doit()
num=num+1
end

if time==“00:20” then
doit()
end
[/lua]

Problem is that it will keep on repeating even when the time is at 20 seconds, since a function takes less than a sec to happen.

How can I modify the if statement to make it do the function only once?

code keeps running constantly as long as it has anything to do.

use events to initiate and control code

your code here will only execute the doit function once if you run line 5 at this exact time, and will not repeat unless you run it in a loop, which the code above does not show.

i advice you to look into the use of timer functions in the documentation, specifically timer.performWithDelay()

Small request.  Prettify code posts like you were going to be graded. 

i.e. Go back and re-edit when posting to fix: indention, spacing, etc. 

It makes it much easier for folks to help if we can read the code easily.

local num=1 local function doit() num=num+1 end if time==“00:20” then doit() end

Note: We don’t have the proper context to answer your question. 

Your granularity is no good if you’re looping.  

One hacky way to fix this is:

local num=1 local ignoreDoit = false local function doit() if(ignoreDoit) then return end num=num+1 ignoreDoit = true timer.peformWithDelay( 1500, function() ignoreDoit = false end ) end if time==“00:20” then doit() end

Ah thanks guys for the help!

I realised I should have made my example a little less vague.

I am using true and false statements with the if statement as @roaming has shown

Thanks!

I’ll probably come off as a nagging nanny, but I cannot emphasize enough the importance of spending some time with the corona documentation. It is truly one of the great gems corona brings to the table. Its easy to browse and systematically organized. 

After that, get and browse this one

https://www.lua.org/pil/

It will save you a lot of time in the long run  :smiley:

code keeps running constantly as long as it has anything to do.

use events to initiate and control code

your code here will only execute the doit function once if you run line 5 at this exact time, and will not repeat unless you run it in a loop, which the code above does not show.

i advice you to look into the use of timer functions in the documentation, specifically timer.performWithDelay()

Small request.  Prettify code posts like you were going to be graded. 

i.e. Go back and re-edit when posting to fix: indention, spacing, etc. 

It makes it much easier for folks to help if we can read the code easily.

local num=1 local function doit() num=num+1 end if time==“00:20” then doit() end

Note: We don’t have the proper context to answer your question. 

Your granularity is no good if you’re looping.  

One hacky way to fix this is:

local num=1 local ignoreDoit = false local function doit() if(ignoreDoit) then return end num=num+1 ignoreDoit = true timer.peformWithDelay( 1500, function() ignoreDoit = false end ) end if time==“00:20” then doit() end

Ah thanks guys for the help!

I realised I should have made my example a little less vague.

I am using true and false statements with the if statement as @roaming has shown

Thanks!

I’ll probably come off as a nagging nanny, but I cannot emphasize enough the importance of spending some time with the corona documentation. It is truly one of the great gems corona brings to the table. Its easy to browse and systematically organized. 

After that, get and browse this one

https://www.lua.org/pil/

It will save you a lot of time in the long run  :smiley: