What I’ve been doing is using local scripts to call an event when you verbally say a spell (and press a key bind) but I was looking into module scripts and it seems more simpler but I have zero idea on how to use. Can anyone enlighten me?
Yesterday I made an extremely similar thing.
Modules are not so necessary in this case (OOP is not so necessary, having a module to activate spells could be useful only if you need to activate spells through multiple scripts), I just had a dictionary table containing pairs of a key as the name of the ability (in your case spell) and a function as the value. Once the player presses a constant key bind, I will reference the function through the dictionary (I have a string-variable for the current ability), and call it.
Example:
local userInputService = game:GetService("UserInputService");
local spells = {
["Spell 1"] = function()
-->> Spell 1's code.
end,
["Spell 2"] = function()
-->> Spell 2's code.
end,
-->> And so on.
};
local currentAbility = "Spell 1";
function InputBegan(input, gpe)
if not gpe then
if input.KeyCode == Enum.KeyCode.B then
abilities[currentAbility](); -->> currentAbility would be changed according to whatever the player is currently selecting.
end;
end;
end;
userInputService.InputBegan:Connect(InputBegan)
This was my approach, there is certainly a more efficient approach, but it is efficient enough to be used in a game, as well as simple.
I see what you’re saying here but I was thinking more of having all my keybinds in one script that individually fire the event which then calls on the module. Does that make sense to you?
Having a table which within there are tables, and each contains values of KeyBind and Activate function.
When a new input is received, loop through the tables, and find a spell corresponding with that KeyBind, if it is found, call the Activate function. You can definitely find an approach using modules to, but it is not essential, and will potentially complicate the task.
Well module scripts are just a table, they behave in the same exact way except they are “global items” and to use some of the items to will need to “require” the module so like local Module = require(game.ReplicatedStorage.Module)
Lets say you have a value in there so to access it use Module.ItemName
or require(game.ReplicatedStorage.Module).ItemName
and can be used as functions, there are two ways of adding items in a module table.insert
and module.Item = something
and you can loop through them, adding a function in a module is aswell simple, most people like to write it as
function Module.Function(a)
print(a)
end
In the most simple words, it is a global table and can be accessed by the client and the server
Now would I be able to simply transfer my already completed spells into a module scritpt?
Yes, it is very simple, especially if it’s a function. Before that tho, do some module script “Training” before implementing anything so you dont mess up.
Thank you for the help. Both of you!