Which is best for performance?

Hi, I’m currently away from Studio at the moment, so unable to go too much into details but I have a question;
I’m working on wands that work by the Player chatting a spell; it then checks to see if the Player has that spell’s ModuleScript as a Child; if so, it requires that spell and casts it.

Logically speaking, this would mean each Player has a group of individual ModuleScripts all being fired at separate times throughout the course of the server,

The performance friendly idea I’ve had is substituting the individual ModuleScripts for Values; that way, the Wand will check to see if the Value is present and if so, it will require the single ModuleScript from ReplicatedStorage and cast the spell that way.

Once again, logicially speaking - I’ll be removing 50+ ModuleScripts that will be within the Players Wands and substituting them for values.

Both of these seem like an unnecessarily excessive uses of instances. Is there any reason you can’t have one module return a table with the indexes being the spell name and the value being the spell function? I do something similar to that for my admin script, which is a similar concept. In case I explained badly, here’s an example of how the structure could look:

local module = {}

module["Castius Exemplia Spellius"] = function()
	-- get mouse position and send it to server
	--(or whatever it is that you do in each individual module)

module["Some other spell"] = function()
	-- maybe this would be a more selective spell/gamepass, so check if allowed to cast it or return nil

return module

I like this concept - The way my spells work themselves is by:
Requiring the SpellModule - This will then create the arguments for: SpellColour, SpellSound, Animation - This will then be passed into a second module that physically creates the spell serverside, with the necessary details received from the prior Module.

Upon hitting a Player / Object, the CreateModule will then return to the original SpellModule in order to retrieve the effects of the spell, such as damage, effects etc.=

1 Like

You should just put the spells in a table, like what @posatta said. If you want the spells to have properties, just put them in a table, and assign it as the value of the key spell name:

local spells = {
    ["Abracadabra"] = {
        ["SpellColor"] = spellColor,
        ["SpellSound"] = spellSound,
        ["SpellAnimation"] = spellAnimation},
    -- so on

You can then check if the spells exist by simply doing:

if spells["Abracadabra"] then
    -- runs if spells["Abracadabra"] is not nil

You don’t even have to put them in a modulescript. Helps with performance, and it won’t be such a nuisance too :smile:.

Even better, if you know some OOP, you could do something like this:

Spells = {SpellName = nil, SpellSound = nil, SpellAnimation = nil}

function Spells:new(SpellName, SpellSound, SpellAnimation)
    spellObject = {}
    setmetatable(spellObject, Spell)
    self.__index = self
    self.SpellName = SpellName
    self.SpellSound = SpellSound
    self.SpellAnimation = SpellAnimation
    return spellObject