Hi, I saw on wiki regarding coroutine.wrap and I’m just wondering in what case would I use this?
I understand how a function can be wrapped in it then called later, but what’s the difference from just calling a function normally e.g. lua"repeatThis()" and putting in a lua local f = coroutine.wrap(repeatThis) then calling lua"f()"
Just trying to get used to understanding coroutines and knowing where I can use them, I humbly ask if anyone could also elaborate on the other coroutine methods in the wiki in a scenario that would be necessary to use them coroutine | Roblox Creator Documentation
Code inside coroutine.wrap runs in a different thread. One use case is running loops without delaying the script or having to make multiple scripts.
example:
for i = 1, 25 do
coroutine.wrap(function()
while task.wait(.2) do
print("hi i'm loop "..i)
end
print("doesn't run")
end)()
print("runs cause it's outside the loop thread")
end
This post provides a great explanation related to coroutines.