Hi all,
I’m currently running an issue in my game where when I call a modulescript function, instead of roblox creating an instance of the function and running it, it directly runs it instead, which because the modulescript function I called is an infinitely looping function, so any code written beyond the calling of the modulescript function would never run.
Here’s my code for reference:
if pressedButtons == totalButtons then
elevatorFunctions.departElevator() -- this is an infinitely looping function
end
task.wait(0.1)
print("it runs")
updateButtonCount()
Because elevatorFunctions.departElevator() is an infinitely running function,
would never run. This issue could be solved if the game creates an instance of elevatorFunctions.departElevator() function and runs that instead, except the only issue is I don’t know how to do that, so any help with this would be greatly appreciated. Thanks in advance!
sorry can you explain a bit more? you use a normal server script to call a function of a module script, that will keep forever calling itself and the normal script has to wait until it finishes (never) to keep going? maybe try using a spawn() or a couarentine to use the module function in another thread, or maybe move the module calling line to the bottom, or re-think the scripts logics, can you show more about the “elevatorFunctions.departElevator()”?
So, what you’re simply saying is, you want the elevatorFunctions.departElevator() to run without making the script wait for the function to end? If so, you can use task.spawn() or spawn() to execute it.
task.spawn() simply creates a new Thread, which is kind of like an Instance as you would call it. This thread can be used to execute your function in the ModuleScript. So you could do:
if pressedButtons == totalButtons then
task.spawn(elevatorFunctions.departElevator) -- this is an infinitely looping function
end
You could also look into Coroutines, as it explains Threads much better than I do.