Wait/delay function

I need a function or some sort of way to wait or delay an amount of seconds and then move on. I know there is timer.delay and have tried it but I might be doing it wrong.

What happened? Did it not work? Did you get an error? Maybe you could show your code?

You have some examples of timers here:

I didn’t really get an error, it just didn’t work and I think I just did it wrong. also I don’t want it to repeat or wrap it into a function or anything I just want something that I can do to wait an amount of seconds and then move on.

Why not? It’s easy to do with an anonymous function like this:

timer.delay(5, false, function()
    print("Hello, world!")
end)

Pausing the script in the middle of execution with some kind of sleep() function wouldn’t work, because scripts are run on the same thread, and so that would freeze the entire game for the duration. It’s possible to get a similar code style using coroutines, but timer.delay() is much simpler.

1 Like

It’s mainly for readability and ease of use. Ill more than likely need to use it a lot and i really just need some way of waiting or delaying in an easy way.

You can run your code within a coroutine and use a timer, wait and resume when the timer triggers. You have a module that is ready for this kind of use here:

3 Likes