I have a weapon system that uses tools and a custom weld implementation, right now, weapons are created from a modulescript that describes it in a one simple table (~6 values), and managed by 2 master scripts (client & server), the modulescript itself doesnt get cloned, it breaks down into number values, string values, etc. Though I’d like to include a few callback functions in it, this makes the whole value thing pointless; would require()-ing the module very frequently (e.g. on fire/equip to get it stats) decrease performance? Im assuming if 20 people at once all shoot their machine guns server (and possibly client) will have to require a lot.
nope, after requiring once the module gets cached and always returns the same value and it doesn’t have a performance impact unless u are requiring using asset IDs
very relieving to hear, what about identical modules, there would be a lot of same weapons around
require() returns what the ModuleScript returned, and runs it once if it hasn’t run yet.
You most likely don’t need to change anything, however if performance is a concern, you may want to cache the returned values in a table, as working with Instances is quite slow compared to just indexing a table.
they all get cached separately but u still shouldn’t worry about performance when it comes to modules
if i’d store an object value pointing to a module script located in replicated storage that belongs to the weapon would that do any better?
it wouldn’t make it any slower or faster and honestly at that point just put the modulescript in the weapon lol
Indexing instances is pretty slow, and that most likely won’t help much unless your path tree is really deep. We’re talking microseconds here of course (maybe less), but it can add up. If you want to optimize for performance you should only call require() once per module, and cache the returned values in one table.
the performance impact of requiring over and over just because of indexing the module object is extremely minor, saving all requires in a table is more of just a quality of life thing
so i would require() all the weapons once put them in a table with name as key and use it when said weapon does something?
yeah u can totally do that if u don’t plan on putting the modulescripts in the weapon tool or something, it really all comes down to how u plan to structure ur game
That would be the optimal way to do it, though if you aren’t experiencing performance issues currently you do not need to change anything!
i’d honestly prefer that, it just feels wrong to require over and over ![]()
While require() does only run the given ModuleScript once, it is still pretty slow (compared to indexing a table) to search for the module, then call require on it again.
it’s absolutely okay to require modules over and over since it always returns the same value without rerunning the code and like I said the performance impact of indexing the module object is extremely minor, I have thousands of re requires to the same modules in the same game and haven’t experienced any issues with performance caused by it ever
but ofc keeping it all in a single module looks way better than having the same couple of requires at the top of ur script
just remember it all comes down to how u structure ur game
Instances in Roblox are userdatas which point to the internal object on the C/C++ side, when you do any sort of operation on them they call the respective metamethod for them, in this case the most relevant one is __index. This can actually get pretty expensive, and is the main bottleneck in a lot of scripts. Things get worse when you use Instance:FindFirstChild() to search for modules, it’s 20% slower than the dot operator, and almost 8x slower than storing a reference to the object.
OP here likely isn’t requiring and indexing modules several times per frame of course, but it may be better to optimize now than having to go back and rewrite code if a performance issue does arise from this.
The idea to put your ModuleScripts somewhere common, like ReplicatedStorage (or ServerScriptStorage if it’s server-side) is best in my opinion; and if necessary use an Attribute or StringValue on the Tool to store the name of the common module, or ObjectValue to hold a reference to it.
Or as also suggested in this thread, instantiate all your weapons once, storing the result of the require in a dictionary, and use the Attribute / StringValue on the Tool (or just the name of the Tool) to just tell you which weapon module to call upon from.
Both of those approaches are fine, the main thing is avoid duplicating modules or scripts.
most of weapon’s assets a child of modulescript so im fine
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.