Problems with a move system

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!

game.ReplicatedStorage.Remotes.Moves.Fire:FireServer(“Fireball”) – in this line you are not passing the mouse position so this line must be:

game.ReplicatedStorage.Remotes.Moves.Fire:FireServer("Fireball",mouse.Hit.p)

I am still getting the error. I think I am doing this part wrong

MagicEvents.Fire.OnServerEvent:Connect(function(player, Move, mousepos) Fire[Move].Fire(player, mousepos) end)

But I don’t know how to fix. I tried [“Fire”] instead of .Fire but it still gave the error as well.

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
1 Like

You are indexing your module’s table incorrectly!

return {
	[1] = Fireball;
	[2] = FireFists;
}

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
1 Like