This is quite a long thread and a pretty open topic, hopefully my explanation below can be understood, hopefully at the end I’ll get some response on this long boring topic… lol
So lately I’ve been wanting to optimize my client main code a bit more for better overall performance.
In my game, I put everything inside a big local script. Along with few module scripts with functions that are frequently called. However, other than that, everything else is being STORED in the big client script, such as input data, character states etc. with around 6K lines in total. These things can be pretty wide, such as custom player list, custom backpack, vote-kicking player handler etc.
There are 2 reasons why I wanted to do some optimization on my client script:
Firstly, I have noticed few performance problem.
For an example, I have a custom footstep framework for my game, it checks every players’ character state and character velocity every step (I know there’s better approach). Recently, I’ve moved the footstep framework FUNCTIONS from my big client script to a module script, but it’s still being run inside the big script, before, everything was ran inside the big script with no slight problem.
For instance:
The client behavior was found to behave more poorly after this change, one of the most obvious behavior downsight was my gun engine was also handled on that big script, and the gun was lagging so much after this was implemented. After I’ve reverted this behavior, the guns seems to be normal again. I’m not 100% sure that this was the factor that was causing it, but it seems to be at this point.
Secondly, I’m really close towards the 200 local variable limits (actually reached it for few times)
I’ve look up on similar threads on DevForum regarding this problem, later finding out that reaching the limit should not be happening usually. Because of how code should be optimized, the limit should not be reached. Suggestions on resolving it is utilizing module script which is something that I’m already doing. I’ve also tried to categorize my variables but later finding out that this is not a long term solution. Indeed, there are quick solutions such as wrapping threads in do end
or using function()
instead of local function()
but I was advised that local function()
should be used instead of function()
. And again, in a long term scope, I think I should start optimizing them now for future game development.
So what am I doing now?
Obviously, trying to optimizing them with MORE module scripts.
I’ve mentioned that I only use module scripts for functions, but I think I should store data and configurations on module scripts, and different module scripts interact with them to get the data. However, I have few concerns on module scripts:
-
Module scripts can be easily required by exploiters to change certain config data (it will be even easier for them to change when they are a module script rather than a client script)
-
Module scripts contains really redundant variables, such as services, instances etc. (E.g: Each module scripts have to define ReplicatedStorage or a certain instance such as PlayerGui if they want to reach them) I already have like over 15 module scripts already, will that be a problem?
-
Should I use local scripts or module script for code that is dependent from
others? (No other moudle script will require it) I’ve seen Ruddev’s open source battle royale and apparently they utilize local scripts for different aspect of their game. One down sight of local script is it can be disabled by exploiters. -
Different module scripts can initialize at different time, a certain data might not be fetched or avaliable when they firstly initialize, resulting into an error. In a big local script structure, I can order code and decide which runs first such that data integrity is mostly guaranteed.
-
Module scripts can not require each other, I afraid that I’ll create a clash between multiple module scripts when they are requiring each other.
To conclude, I don’t really need a solid answer for my problem, but I just want to hear suggestions from developers out there on how they optimize their game, and most importantly, I want to make sure what I’m doing right now (optimizing with module script) is correct and I’m going to a right direction.