luarobloxluau

How do I make a wait statement in Lua?


I have been using Luau (a scripting language derived from Lua) in Roblox Studio and it has a wait() function, but Lua does not. How do I wait in a loop? I use Lua when I'm offline from Roblox Studio to plan my code. How do I use a wait() statement in Lua?

This is the code I have tried so far. It does not work. This is what I would do in Luau in Roblox Studio, but I'm not sure how to use a wait statement in regular Lua.

while true do
wait()
-- code here

Solution

  • There is a list of sleep/wait functions here.

    One of the easiest to implement is using os.clock().

    function sleep(s)
      local ntime = os.clock() + s/10
      repeat until os.clock() > ntime
    end