Concerns about POSSIBLE performance issues by using loop on enemy NPCS

What do you want to achieve?

  • To give a quick rundown of what I am trying to do, I have a anime game that features enemy NPC’s that fight back and do many other advanced actions. (image of one of the npcs)

What is the issue?

  • I am here to express my concerns if I should be worried about performance issues by having that many server scripts with loops running every 1 - 2 seconds. There is a if statement to see if it’s hit then it will go on a rampage and so on.

Which is why I am here to ask the members of the Dev Forum for feedback, I was thinking of merging it all into just 1 serverscript and going with the idea of 1 loop is better than 10. If there is some other easy way I could really use some help.

Have you thought of using bindable events for hit detection instead of checking with a loop?

1 Like

How would I go about using bindable events for such things? I thought bindable events just work as a regular event that gets fired upon call

When the player hits the enemy, an event would be fired, passing arguments such as which npc is being hit, what kind of attack, etc., then the serverscript handling the enemies listens for the event and does the appropriate response.

2 Likes

If you find this a bit complicated the loop method you suggested would be alright as long as it’s just the one script doing it and you don’t go overboard with the stuff you’re doing inside the loop.

That’s a really good idea, I’ll try that and see what happens. Thanks for the input; if everything goes right I’ll mark this thread as solved or get back to you if something goes wrong.

Wherever possible, avoid loops and go for an event-driven system. While loops aren’t inherently bad to use, there is almost always a case where you can use events instead. That’s where BindableEvents, as aforementioned, step in. Whereas you can have a loop to check if the NPC was hit before, you can have an event yo determine when the NPC is hit.

Event-driven systems are found in every piece of work you will create on Roblox. They are very powerful because they effectively allow you to specify that certain things in your game should happen when a certain action occurs or input is given. As opposed to an occasional loop where the certainty of when the next iteration occurs is indeterminate, you can have an event to activate your actions immediately (near-instantaneously) when a condition is met.

More reading: https://en.m.wikipedia.org/wiki/Event-driven_programming

2 Likes