Click here to Skip to main content
15,888,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
There's no wait statement in Lua, and you cant make it. So yeah, rip
 
Share this answer
 
Comments
CHill60 8-Aug-23 10:26am    
"you cant make it" - except you can. Look at link in Solution 1. Apart from that you haven't added anything to this thread by adding an additioal "solution"
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
 

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