I’m trying to create a spell casting system using a module script, but it’s printing this:
20:19:30.487 Funnyskyswagway3 is not a valid spell. - Server - SpellSystem:18
local Spells = game.ReplicatedStorage.SpellModules
local SpellSystem = {}
SpellSystem.Spells = {
["Teleport"] = {
Activate = Spells.Teleport
}
}
function SpellSystem.Spells.AddSpell(self, spellName, spellFunction)
self.Spells[spellName].Activate = spellFunction
end
function SpellSystem:CastSpell(player, spellName, ...)
local spell = self.Spells[spellName]
if not spell then
warn(tostring(spellName).." is not a valid spell.")
return
end
spell.Activate(player, ...)
end
print(SpellSystem.Spells)
return SpellSystem
local SpellSystem = require(game.ReplicatedStorage.SpellSystem)
local function parseChat(player)
game.Players.LocalPlayer.Chatted:Connect(parseChat(),function(message)
local words = {}
for word in message:gmatch("%w+") do
table.insert(words, word)
end
local spellName = words[1]
if SpellSystem.Spells[spellName] then
table.remove(words, 1)
SpellSystem:CastSpell(player, spellName, unpack(words))
end
end)
end
Your call to :Connect() is misleading. You are actually passing the player’s name as “message” to your anonymous function. I believe your script should look like this:
game.Players.LocalPlayer.Chatted:Connect(function(message, recipient)
local words = {}
for word in message:gmatch("%w+") do
table.insert(words, word)
end
local spellName = words[1]
if SpellSystem.Spells[spellName] then
table.remove(words, 1)
SpellSystem:CastSpell(player, spellName, unpack(words))
end
end)