I want to reduce lag in my game, and I think my usage of modules is causing that. The reason I believe so is whenever a script is done being used (i,e., a weapon is put away and no longer in use) it is still active. For example, a loop would still continue even though I put the weapon away.
The way I have it set up at the moment is whenever the person equips the weapon, the script clones a module from ServerStorage, then requires() the clone, saving the required as a variable, and then deletes the clone. I do that due to my anxiousness over it potentially damaging the game, but my understanding on it might just be completely broken.
If my newer understanding of it which is, only one script should ever require() a module, then have every script using that module just refer to a global variable. That way there would be no more require()'d scripts that’ll just exist for no reason.
For context, the script is cloning these modules:
Also, cool-downs and variables that need to be accessed by other functions are in the Modules, which may be an issue as if I go for the 1 require()'d but shared variable method, it’ll be a massive issue of people sharing cool-downs or sharing those variables. At least, I think so.
Please help me understand this better, and hopefully show me what I’m doing wrong. If the entire thing is a huge “oh yeah no that is super wrong” and I should just make them regular scripts as the best and only real solution, then please explain that to me too! c: I’d like to not make a mistake like this again if that is the case, thank you.
Requiring the same module in two different scripts will cache the result and have it return the same thing after the first time. Instead of putting its return in a global variable, just require it again.
Requiring scripts has no cost. Idle loops (which are just waiting) also have minimal cost, just don’t go making thousands pointlessly.
If you want to reduce lag you need to figure out the exact cause of that lag. The microprofiler is the tool you need to use for this and will show exactly what is eating up your processing time. Most likely there is a loop which does a much more intensive calculation than you expect. If there is nothing showing up on the microprofiler then you are probably yielding (waiting) somewhere you shouldn’t be.
That’s the thing, I think my method is pointlessly requiring because it requires on equip. Which means a new script gets required whenever someone presses 1. Wouldn’t there just be a invisible script floating somewhere with no more purpose? I guess I’m asking how to delete that invisible script floating somewhere.
The problem is I don’t want to do all that recoding for every script unless it’s for certain that it’s the only method to get rid of the issues that my current method is bringing up. There are give or take 50 weapons in the game and would be pretty annoying to do the cooldowns for especially if it leads to nothing. And I do use the while conditional stuff, but for testing to make sure the module does continue even after i delete it, I just used a regular while wait(1) do print sorta deal with no conditional.
What does this mean? I’m not really wise around on the scripting lingo but it seems like you’re saying it’d require the same script, so does that mean I shouldn’t clone it and just require it from it’s ServerStorage location?
Yeah, just require it from its ServerStorage location. You can require it again in a different script and it will have the same result without running anything again.
The way you’re currently handling tools is more of a practice-related problem more than anything. Modules have nothing to do with lag: it’s the code you’re running in those modules that’s contributing to lag. This will especially be a case if you’re heavily dependent on the server to run operations.
Your current structure is fine if it works for you. Just address the performance side of things. If your structure is part of the performance problem, you’ll have to do that over. The MicroProfiler, as previously suggested two times, will help you find an isolated cause of lag and you can take it there from the many debug tools available to you.
Here’s an example profile of your game at a certain frame:
As you can see, worker::runJob is a bar that spans several bars. This can point to the fact that some optimisation on loops can be done. This profile is on a new VIP server though so it’s not representative of any actual major performance problems.
That aside: if you want my two cents, I think that the performance problem is indirectly related to the game’s structure and how modules are cloned. Instead of having cloned modules, you want to aim to have your scripts relatively static. This means no cloning or manipulation, just requiring and acting. How to achieve this level of streamlined tool behaviours differs from one implementation to another.
Alright thank you, I’ll be studying into my game’s microprofile and also not clone the modules anymore. I also noticed that Received Network goes really high with this pulsating effect I have (which is 44 decals/textures having their transparencies tweened) on the server side, which I plan to put on the client side after seeing this Received Network issue.
For sure. Tweening on the server-side is not the greatest idea and it can get fairly choppy when the server is under higher loads. It is typically recommended to perform tweens on the client-side, as well as generally any other aesthetic effect. The client can tween smoothly as well, which is a plus point.
In cases like this, I typically set up a handler on the client of some sorts. The server tells the client via remote to perform a specific effect given some information and the client executes it accordingly.