• Endless loop with 1 second waiting, how?
    8 replies, posted
Hello, i am learning glua but i have a problem which i cannot solve with google. I want too loop a code but i want that it wait's a second till the next loop so it won't overload gmod. How can i do this? I just did not found any solution online. For example i want to execute the function "hi()" every second endlessly. Please help.
timer.Simple
i tried it already but no matter what i do it just won't work.
how about showing the code that you tried but didnt worked
function chatmessage() print( "Welcome\n" ) end timer.Simple( 5, function() chatmessage() end ) It will only print Welcome one time and after that it just stops.
timer.Create
of course it will, you start the simple timer only once, which means it only runs the chatmessage function once. you need to repeat the timer as well. One way you could do this is put the timer inside the chatmessage function. Then you just need to call the chatmessage function once on start, and then it will recall itself every five seconds again because of the timer. function chatmessage() print( "Welcome\n" ) timer.Simple( 5, chatmessage()) end also dont do function() chatmessage() end. That creates a function inside the timer, that starts a function. so you create a function that starts a other function. thats useless here. just write the function you want it to execute
Just do this: timer.Create("AnyIdentifier", 1, 0, functionToCall) And remove it later on if you don't need it anymore with timer.remove("AnyIdentifier") That's the easiest solution. If you don't need the function anywhere else you can even put the function inside of the timer instead calling it. For further help: timer.Create timer.Remove
That didn't work. It spammed my console without wating and then it crashed with some error. [ERROR] stack overflow   1. print - [C]:-1    2. chatmessage - lua/test.lua:2     3. chatmessage - lua/test.lua:3      4. chatmessage - lua/test.lua:3       5. chatmessage - lua/test.lua:3        6. chatmessage - lua/test.lua:3         7. chatmessage - lua/test.lua:3          8. chatmessage - lua/test.lua:3           9. chatmessage - lua/test.lua:3            10. chatmessage - lua/test.lua:3             11. chatmessage - lua/test.lua:3              12. chatmessage - lua/test.lua:3               13. chatmessage - lua/test.lua:3                14. chatmessage - lua/test.lua:3                 15. chatmessage - lua/test.lua:3                  16. chatmessage - lua/test.lua:3 This worked! Thank you so much! Finally my problem is solved. Very simple and nice.
Sorry, you need to Log In to post a reply to this thread.