r/lua • u/Weekly_Flounder_1880 • May 04 '25
Help how do I make a wait( ) function?
hi, I am new to Lua.
at first, I was using Roblox studio, but I moved to Love2D
in Luau (roblox's alternative for Lua), they have a built in wait()command in their library
Now I realised, I don't have a wait() function in my library
it is pretty self explanatory, I want a wait() function that makes the program wait for a set duration of time before executing the following code
11
Upvotes
9
u/Togfox May 04 '25
This belongs in /r/love2d but I'll answer it here:
function love.update(dt) will execute every cycle so you use dt (delta time) to track elapsed time
6
u/Cootshk May 04 '25
love.timer.sleep() will pause everything, but you can implement your own using the dt argument to love.update()
5
u/CirnoIzumi May 04 '25
local t0 = os.clock() while os.clock() - t0 < 1 do end
But don't actually do this, it's bad perfomance.
But since you're in love2d. love.timer.sleep(1)
2
u/bidaowallet May 07 '25
can you use a countdown in seconds to set wait? why not?
2
u/xoner2 May 09 '25
Love2D is a game engine. It should provide facilities for sleeping no?
But just to complete the thread. Here's a sleep function, taken straight from the LuaJIT documentation. Works with PUC-Lua so long as you have compiled the ffi library:
1
u/youre_not_ero May 04 '25
If you're using luaJIT, you can call
sleep()of the underlying os-3
u/AutoModerator May 04 '25
Hi! It looks like you're posting about Roblox. Here at /r/Lua we get a lot of questions that would be answered better at /r/RobloxGameDev, scriptinghelpers.org, or the Roblox Developer Forum so it might be better to start there. However, we still encourage you to post here if your question is related to a Roblox project but the question is about the Lua language specifically, including but not limited to: syntax, language idioms, best practices, particular language features such as coroutines and metatables, Lua libraries and ecosystem, etc. Bear in mind that Roblox implements its own API (application programming interface) and most of the functions you'll use when developing a Roblox script will exist within Roblox but not within the broader Lua ecosystem.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
-3
u/AutoModerator May 04 '25
Hi! It looks like you're posting about Love2D which implements its own API (application programming interface) and most of the functions you'll use when developing a game within Love will exist within Love but not within the broader Lua ecosystem. However, we still encourage you to post here if your question is related to a Love2D project but the question is about the Lua language specifically, including but not limited to: syntax, language idioms, best practices, particular language features such as coroutines and metatables, Lua libraries and ecosystem, etc.
If your question is about the Love2D API, start here: https://love2d-community.github.io/love-api/
If you're looking for the main Love2D community, most of the active community members frequent the following three places:
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.