How to run a function multiple times without them interfering

Hello,

I’m making a script that can move multiple zombies at once. I only want to use one script for all of the entities to make the game less laggy. (The animations and visual stuff are already being calculated on the client instead of the server)

I’ve tried using 1 module script with all the path find functions and then send which monster should move to that module script. I do use some wait()'s in the code. The problem is that the function gets run multiple times and it gives some weird results.

So i’m wondering if there is a better alternative to run a function on a specific zombie to make it move for example, without interfering with all the other running functions.
I’ve heard of coroutines but I’m not sure if running hundreds of coroutines is efficient. Also, when a function is fired in a module script multiple times from the same script, do wait()'s yield all the other running functions or are they all separate threads?

1 Like

Try giving coroutines a try regardless, then stress test it

(An alternative to coroutines is spawn(functionName)

2 Likes

I tried it with 2 zombies and it works perfectly! But can massive amounts of coroutines drop performance?

1 Like