Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to create spells using module scripts?

Asked by 2 years ago
Edited 2 years ago

What I’m trying to achieve here is the usage of spells through module scripts. What my problem is I don’t know how to make it so if you say the spell “Incendia” it does what’s in the module script (Second Code) I’ve just started to learn about module scripts so I'm stuck on this. If you need more clarification I will provide

local Code = {}
local Players = game.Players
local ReplicatedStorage = game.ReplicatedStorage
local Modules = ReplicatedStorage.RModules
local TweenModule = require(Modules.Tweening)
local Assets = ReplicatedStorage.ReplicatedAssets
local WitchFolder = Assets.WitchesAssets
local AnimationsFolder = Assets.Animations
local EffectsFolder = Assets.Effects
local SoundsFolder = Assets.Sounds
local RemoteEvents = ReplicatedStorage.RemoteEvents
local PlayAnimationEvent = RemoteEvents.AnimationClientEvent

local t = 1
function Code.Execute(Character,ContentTable)
    local Player = Players:GetPlayerFromCharacter(Character)
    local Target = ContentTable.Target
    local TargetPlayer = ContentTable.TargetPlayer
    local Damage
    Damage = 2

    local fire = ReplicatedStorage.FireParticle.Fire:Clone()
    fire.Parent = Target.UpperTorso

    local result = 15 * t
    for t = 1,result,1 do
        Target.Humanoid.Health -= Damage
        wait(0.2)
    end
    fire:Destroy()
end


return Code

Here's the second code:

local ReplicatedStorage = game.ReplicatedStorage
local Storage = ReplicatedStorage.SpellsModules
local Spells = {
    ["Incendia"] =
        {
            Launch = Storage.Incendia,
            Input = Enum.UserInputType.MouseButton1,
            Cooldown = 8,
            Spell= "Incendia",
            Magnitude = 30,
            Magic = 60,
        },
    }
return Spells

To find the spells name when you say it in chat:

local ClickEvent
local WitchSpellEvent = RemoteEventFolder.WitchSpell
local function WitchTyped(msg)
    local SpellsModule = require(ReplicatedStorage.r.SpellsConfiguration)
    local FilteredMessage = msg:lower():gsub("\r","") 
    print("Client said ''"..msg.."''")
    local function IdentifyTarget()

        local Target = Mouse.Target
        if Target.Parent == nil then return end
        if Target.Parent:FindFirstChildOfClass("Humanoid") ~= nil then
            return Target.Parent
        elseif Target.Parent:IsA("Accessory") and Target.Parent.Parent:FindFirstChildOfClass("Humanoid") ~= nil then
            return Target.Parent.Parent
        end
    end
    for SpellName,Table in pairs(SpellsModule) do
        local incantation = Table.Key:lower()
        local inputType = Table.Input
        if FilteredMessage == incantation then
            if inputType == Enum.UserInputType.TextInput then
                WitchSpellEvent:FireServer({IncantationText = incantation})
            elseif inputType == Enum.UserInputType.MouseButton2 then
                if ClickEvent ~= nil then
                    ClickEvent:Disconnect()
                    print("disconnected last event spell!")
                end
                ClickEvent = UserInputService.InputBegan:Connect(function(Input,Processed) 
                    if Processed then return end
                    if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
                        local target = IdentifyTarget()
                        print("Clicked!")
                        if target ~= nil then
                            WitchSpellEvent:FireServer({IncantationText = incantation,Target = target})
                            ClickEvent:Disconnect()
                            print("sent signal!")
                        end
                    end
                end)
            end
        elseif string.find(FilteredMessage,incantation) ~= nil and Table.Input == Enum.UserInputType.InputMethod then
            if string.split(FilteredMessage,incantation.." ") ~= "" then
                --print(incantation)
                --print(string.split(msg:gsub("\r",""),incantation.." ")[2])
                local char = string.split(msg:gsub("\r",""),incantation.." ")[2]
                WitchSpellEvent:FireServer({IncantationText = incantation,TextTarget = char})
            end 
        end
    end
end


Player.Chatted:Connect(WitchTyped)

1 answer

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
2 years ago

What you have so far is a very nice approach by not having to repeat your code. The only thing I would do if I were to make this myself is to define the spell's configuration in the same module script you use to contain the spell's function. It's more convenient that way because you don't have to hop from two scripts, one being a probably massive master module for your spell's configurations.

I will take the current setup you have into consideration as I am unaware of your usage of this. In the master spell config module, I would just run the module script and get the execute function from there. Considering that you mentioned that you desire to make the spell get activated when they say the spell's name, then I would consider the following. Whether or not you literally want to do this, you should still consider it just in case: I would probably have an Activator index that specifies the type which is a string, whether that is input (the type could be "input") or through a chat message (the type could be "chat_message"):

local ReplicatedStorage = game.ReplicatedStorage
local Storage = ReplicatedStorage.SpellsModules

local Spells = {
    ["Incendia"] =
        {
            Launch = require(Storage.Incendia),
            Activator = {
                ["type"] = "chat_message",
                ["content"] = "incendia"
            },
            Cooldown = 8,
            Spell= "Incendia",
            Magnitude = 30,
            Magic = 60
        }
    }
return Spells

And for how you call them, for example:

for _, spell_config in pairs(MasterSpellConfig) do
    local activatorConfig = spell_config.Activator
    local activatorType = activatorConfig.type

    if (activatorType == "input") then
        -- use InputBegan connection and call whenever it is appropriate
        UserInputService.InputBegan:Connect(function(input)
            if (input.UserInputType == activatorConfig.input) then
                spell_config.Launch.Execute()
            end
        end)
    elseif (activatorType == "chat_message") then
        -- use player.Chatted event
        Player.Chatted:Connect(function(msg)
            if (msg == "content") then
                spell_config.Launch.Execute()
            end
        end)
    end
end
0
I seem to be a little confused because im trying to get this to work but it's not working Funnyskyswagway3 30 — 2y
Ad

Answer this question