how much can a server script be considered a lot? here I show a complex system with pathfind calculation, sound listening, path prediction, different animations, many events and more.
The lower you can get a script, regardless of where it is and what it is doing, the better. I want you to understand that you can “Code anything you want.” My best advice is to look at what you are doing in the script and if you can break down anything better to run more efficient. There is not set “%”, aim as low as you can go.
the script itself does not consume anything, but the problem is that I have to duplicate it for 160 more npcs, and it is 500 lines of code, I saw that there were ways to make my script not have to be copied and pasted in each NPC model, but I don’t know if it will help with the optimization or if there is another way.
I don’t see 160 NPCs, in the video you should test it with 160. What are you using to manage all these npcs? What services are you using? If your script goes to 40% and you expect to run 160 NPCs I fear your pc will die and anyone playing.
This needs to be changed never copy paste scripts into something if you can help it. You need to look at collection service.
In the first video, it is with only one npc, the second picture I sent that shows the very high percentage, that is with 160 NPCS. I am using the SimplePath module, to verify the proximity of the player to the npc I use Magnitude and while true, then to perform the hit and take damage to the player, I check if the distance of the npc is less than the maximum attack range and if so I run a function which does damage to the player, plays sounds and animations. also if the player goes in a straight line (for about 1 second) then I recalculate the path to intercept him and not always be following his back. If the player is not close then I make a path to a random point on the map, and if the NPC loses the player, then the function is executed to make a path to a random point on the map.
I tried collectionservice, but the script was still consuming me a lot
Collection service running the same as normal scripts sounds very weird to me. I am not sure, that worries me. I know of simplepath but let me refresh myself on it I will go look at the module.
I recommend first, to eliminate the issue of simplepath not running correctly, try and use another path finding module. I see like 4 of them in roblox resources and that one is almost 4 years old or somewhere around that. I know it works well with 10 NPCs, but 160 you will have to start really looking at how these modules work. Testing different ones etc. Try NoobPath and if that doesn’t help reduce the %, test others I know some are way more in-depth, but are harder to use.
I’d recommend looking into parallel lua
I will try everything you are telling me, but I will also try to readapt the whole code to use collectionservice, thank you very much.
Yeah I will say I ran into a similar issue few years ago and made a post, and everyone told me to change everything I can to collectionservice.