1 module that has a while loop inside and a normal script that fires the module
The issue:
If I want to fire that same module twice, basically firing to while loops at the same time, it yields until the first while loop is done. Basically, I want to fire it and not yield. Kind of like a coroutine.
Module script:
local moduleScript = {}
function moduleScript.loopFunction(number)
while true do --yes I know, an infite loop
print(number)
wait(1)
end
end
return moduleScript
Normal script:
module = require(game.Workspace.ModuleScript)
module.loopFunction(1)
module.loopFunction(2) --this will not print, as it yields for the function above to finish
local moduleScript = {}
function moduleScript.loopFunction(number)
spawn(function()
while true do
print(number)
wait(1)
end
end)
end
return moduleScript
local moduleScript = {}
spawn(function moduleScript.loopFunction(number))
while true do --yes I know, an infite loop
print(number)
wait(1)
end
end
return moduleScript
Edit; you posted the script at the exact same time. You beat me to it!
As others have suggested, you need to choose an asynchronous solution to your problem. While spawn is simple and works fine when used sparingly, you should avoid it in favor of coroutines. What @hell_ish suggested (coroutine.wrap) will likely be the simplest solution to your problem and is generally better than spawn, but depending on what you’re doing in these loops there may be a better solution.
RunService.Stepped could potentially be better if these are tight loops (running very often) or if they need precise timing. If these are updating a visual indicator you’d want RunService:BindToRenderStep. If this is just regular game coordination (like waiting 30 seconds between rounds, etc) then doing what you’re doing is fine.
Spawn has a wait() before your function is actually ran.
If you wanna use spawn just because I guess it’s easier or more readable, you can just replace the spawn function with this.
local function spawn(func, ...)
return coroutine.wrap(func)(...)
end
If you need it to run only after your code that summoned it is really, you can use a Heartbeat:Wait() which is short and shouldn’t care any issues.
You probably wouldn’t understand how coroutines work, so this is useless to you.
local RunService = game:GetService('RunService')
local function spawn(func, ...)
return coroutine.wrap(function()
RunService.Heartbeat:Wait();
func(...)
end)()
end
Overall just avoid spawn and you should be fine performance wise.
@LucasTutoriaisSaimo@Ozzypig@heII_ish
Thanks for your answers, and my apologies for such a delayed reply!
After @Ozzypig’s recommendation, I have chosen to use coroutines instead of spawn(). But how would I use it in my script, I tried calling the function/module like this: