Click here to Skip to main content
16,004,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello, so i have been using roblox luau  a custom version of lua and it has a wait() function** however, lua does not. My question is **how am u supposed to create a wait in a loop? i use lua when i'm offline of roblox studio and i use it to plan my code. Does anyone know how to use a wait() statement in lua?**


What I have tried:

looking up how to use it but still confused
Posted
Updated 8-Aug-23 4:14am

Lua has no way to wait function because (I suppose) it is a OS dependent feature.
You may rather easily implement it wrapping the one your OS provides (e.g. usleep on Linux or Sleep on Windows), see Programming in Lua : 26[^].
 
Share this answer
 
function wait(seconds)
local start = os.time()
repeat until os.time() > start + seconds
end
--USE IT LIKE ROBLOX
--Maybe roblox didnt want you to custimize time function because IO and OS function is very powerful
--hacker can use io.open() to create a backdoor script
 
Share this answer
 
v2
Roblox's variant of Lua, 'Luau', can use the `wait()` function.

This Lua 5.4 solution is similar to the previous solution, but I've included a simple but complete sample program to run it:

local user_input = ""
local q<const> = "q" 
print("Press <q> to exit..") 
while string.lower(user_input) ~= q do
  io.write("> ")
  user_input = io.read()
  if user_input == "" then 
  else print ("> You wrote '".. user_input .."'")
  end -- if
end -- while
print("> Goodbye!")

local clock = os.clock
function sleep(n)
local t0 = clock()
while clock() - t0 <= n do end
end
--
sleep(2) -- 2 second delay before closing program
 
Share this answer
 
The easiest solution would be to use the lua API (or even better the Luau API released by roblox). It is for people who wish to add anything that they want to lua whilst running the VM inside of C or C++.

Roblox did this by introducing a function called "wait" and just yielding the thread for a certain amount of time. For the person who posted before me saying "and you can't make it"... yes you can, you can introduce anything you want to lua with this solution.

I'll be honest, it's quite a difficult solution. But if you want it so badly, there is no other way to directly embed a "wait" function in lua's global environment.
 
Share this answer
 
Comments
Richard Deeming 9-Sep-24 4:18am    
As already explained in Solution 4, which was posted over a year ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900