I have a script that calls a function on an event, it’s on the server and I will call it multiple times for multiple players, am I supposed to wait for one to finish before calling it again? If so how can I get around this?
It depends on the function. I’d say you should wait for it to finish if it doesn’t yield the thread. Try to avoid using multiple threads as well. If you do use multiple threads, try running them in parallel (if compatible).
It is much better to not wait for other functions to finish. This could be called decoupling code and is a big help in speeding up scripts. Some functions yield meaning when called they will make your code wait and TLDR: that can be a problem.
Lua is single-threaded everything done in Scripts will happen one step at a time. When using task.wait
or any yielding function the Script gives up control for a while and lets other code run, this is often a bad thing because other code can edit variables and create unpredictable behavior; like if a player logs out while the script is trying to award points, suddenly the server cannot find out who scored for which team! On the other hand some times you need time-based events to happen 2 seconds after another, just make sure you aren’t relying on variables that could change in 2 seconds.