Make a bell ring at a specific time

Started by Juliet119, Jun 03, 2023, 06:26 AM

Previous topic - Next topic

Juliet119

I'm making a clock tower with a bell and a computer hidden inside. I want the bell to ring 6 times every morning at 6. I know this has something to do with os.time() so I tried this:
while true do
    if os.time() == 6 then
          ringBell(6)
    end
end
I tested the ringBell() function separately and it works fine, and the program doesn't throw any errors when run. I'm new to Lua and coding in general. Can someone tell me what I'm missing here?

SamH

I'm assuming you are getting the "Too long without yielding" error. Computers in CC need to let other computers in the world execute their code (even if there are no other computers) so they need to "yield".

To "yield" you can simply do this ...
while true do
    if os.time() == 6 then
          ringBell(6)
    end
    os.sleep(0.01) -- This will end up calling os.pullEventRaw for a timer. os.pullEventRaw will end up calling coroutine.yield on the "master" coroutine.
end