So I’m trying to make a system that happens when you click, you preform a move. Right now I’m testing it out and I get this bug: attempt to call field ‘?’ (a nil value) Here are my scripts:
Module Script:
local Fireball = {}
Fireball.MoveNumber = 1
Fireball.Description = "Unleash a ball of flames upon your opponent!"
Fireball.Fire = function(player, mousepos)
--Script move stuff here
print("Fireball")
end
local FireFists = {}
FireFists.MoveNumber = 2
FireFists.Description = ""
FireFists.Fire = function(player, mousepos)
--Script move stuff here
end
return {
[1] = Fireball;
[2] = FireFists;
}
Server Script (I think the problem is in here):
local MagicModules = game.ReplicatedStorage:WaitForChild("MagicModules")
local MagicEvents = game.ReplicatedStorage.Remotes.Moves
local Fire = require(MagicModules.Fire)
MagicEvents.Fire.OnServerEvent:Connect(function(player, Move, mousepos) Fire[Move].Fire(player, mousepos) end)
Local Script:
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
game.ReplicatedStorage.Remotes.Moves.Fire:FireServer("Fireball")
end)
What’s supposed to happen is when I click its supposed to print “Fireball” but I get the error [attempt to call field ‘?’ (a nil value)] instead. Any help would be appreciated!
Read up on how to use module script your not using them right. I dont have time to elucidate the concept right now but it seems like your attempting object oriented programming. this is how the module show look for example:
--modulescript
function module.CreateAFireClass(MoveNumber,player,mousepos)
local FireClass = {}
FireClass.MoveNumber = 1
FireClass.Description = "Blah blah blah"
FireClass.Fire = function(player,mousepos)
--Some code
end
return FireClass
end
The only way you would be able to index the Fireball table in your Fire module is with [1], like Fire[1].Fire(...). If you want to index it using "Fireball", you will have to do so in your module’s table using the field inside the brackets:
return {
["Fireball"] = Fireball;
["FireFists"] = FireFists;
}
-- this one is also useful and functionally identical:
return {
-- the index = the table
Fireball = Fireball;
FireFists = FireFists;
}
And just so you know, you can construct your tables with fields already included, like so:
local Fireball = {
MoveNumber = 1;
Description = "Unleash a ball of flames on your opponent!";
Fire = function(player, mousepos)
--Script move stuff here
print("Fireball")
end;
}
local FireFists = {
-- you get the idea
}
In fact, you can return your module table with the fields included too:
local FireClasses = {
Fireball = {
-- stuff from last block here
};
FireFists = {
-- you get the idea
};
}
return FireClasses