Hello, I’ve been using lua for a few years now but I’ve never really dove into coroutines that much and mostly avoided them since with roblox’s task
library I never really needed another way to thread my code.
However I’ve tried implementing it recently and the script works but I don’t know if its efficient or if I’m even using it correctly. I’ve placed it in my script for more efficiency.
For example I have a module that has an .enableGui
and .disableGui
function that both turn visibility on and off. But also I have a loop going that gives this certain gui some animation and this loop is in a coroutine that pauses whenever the gui goes invisible and resumes once its visible. I’m guessing this is more efficient as the only other option would be to place some sort of repeat task.wait() until
which I think is quite inefficient or just run the function again which is a bit annoying to handle.
Anyone with good knowledge of coroutines please give some insight. I’ll paste a simple version of my script below
local module = {}
function module.enableGui()
gui.Visible = true
coroutine.resume(updateThread)
end
function module.disableGui()
gui.Visible = false
end
updateThread = coroutine.create(function()
while task.wait() do
if gui.Visible then
-- do animation
else
coroutine.yield()
end
end
end)
return module