r/lua • • Apr 10 '22

VSCode Lua don't have wait.

So, I make code that's supposed to make Hello, World! every second and it don't work as there's no wait. I'm used to having wait in Roblox studio. As that wait is task.wait(Insert seconds here) and if you do it all it gives is wait is a nil value.

0 Upvotes

12 comments sorted by

16

u/y444-gd-acc Apr 10 '22

Vanilla Lua doesn't have wait/sleep functions. The function from Roblox studio will not work. Google for solutions on how to make your own wait function.

1

u/vinnieleverton Apr 10 '22

Oh

2

u/MouKuBi Apr 10 '22

luv library is what you need.

9

u/UziYT Apr 10 '22

wait is a roblox lua function, not in vanilla lua

3

u/AutoModerator Apr 10 '22

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.

2

u/fatboychummy Apr 10 '22

Hello! Many have told you similar already, but I wish to add:

Roblox doesn't actually run Lua, it runs LuaU. It's a custom version of Lua written by the team at Roblox in order to accomplish specific goals.

A lot of things you can do in LuaU, you cannot do in Lua.

4

u/[deleted] Apr 10 '22

This might help:

local clock = os.clock

function sleep(n)
   local t0 = clock()
   while clock() - t0 <= n do
   end
end

 --Usage--
print("Test1")
sleep(2)
print("test2")

Source:

https://stackoverflow.com/a/13263109/18443318

12

u/lambda_abstraction Apr 10 '22 edited Apr 10 '22

At least on Linux, os.clock() gives you the total CPU time across all OS threads, so if your platform with Lua support has spun off other OS level threads, the loop may well terminate early. Also, as busy-waits eat CPU just to create a delay, they are not generally polite strategy for delays. The correct answer is to call out (via a custom module) to your system's delay function, e.g. sleep() or usleep() on Linux. If the underlying Lua is LuaJIT, your access to the system delay function is trivial with FFI/CDEF.

ffi=require 'ffi'
ffi.cdef 'int sleep(int);'

for i=1,10 do
   print 'Hello, world!'
   ffi.C.sleep(1)
end

5

u/[deleted] Apr 10 '22

Never knew, thanks for clarifying! The lore’s crazy on this stuff

3

u/[deleted] Apr 10 '22

luasocket has a sleep function, there is a sleep package on luarocks

1

u/Fit_Strawberry7991 Apr 14 '22

function wait(time)

start = os.time()

repeat until os.time() > start + time

end

1

u/ActiveScript Mar 07 '23

I can relate to that, im trying to find a tutorials but its very hard to find