Indexed function from table of modules does not receive data

  1. What do you want to achieve?
    I’m making a fighting game with a class system, each classes specific move functions are located in module scripts for each one. I want to index a table with the required module scripts and its appropriate movement function.

  2. What is the issue?
    The function index does execute the function, but it doesn’t send the data given from the function call, to the function.

Server Script:

local repStor = game:GetService("ReplicatedStorage")

--ClassModules
local classModuleFolder = repStor.Modules.Combat:WaitForChild("ClassModules")
local tempClassModule = require(repStor.Modules.Combat.ClassModules:WaitForChild("Guardian")) -- for testing

local classes = {}
for index,module:ModuleScript in pairs(classModuleFolder:GetChildren()) do
	classes[module.Name] = require(module)
end 

--Bindables/Remotes
local combatDir = repStor.Events.Combat
local executeMove = combatDir.ExecuteMove

executeMove.OnServerInvoke = function(plr:Player, move:string, class:string)
	coroutine.wrap(function()
		local char = plr.Character
		local hum = char:FindFirstChildOfClass("Humanoid")
		classes[class][move](plr)
	end)()
end

Module Script:

local moves = {}

local repStor = game:GetService("ReplicatedStorage")
local combatInfo = require(repStor.Modules.Combat:WaitForChild("CombatInfo"))
	
function moves:hit1(plr:Player)
	local char = plr.Character
	local rightArm = char:FindFirstChild("Right Arm")
	combatInfo:hitBox(char,rightArm.RightGripAttachment,Vector3.new(2,2,2),true)
end

return moves

Any help would be greatly appreciated.

Instead of moves:hit1(), why not try moves.hit1()?

1 Like

I switched the colon for a period and it worked out, had to do some debugging after that but either way, thanks man.

Silly me, I forgot to explain, sry about that!

Explanation

When you use : in your functions, the function automatically takes ‘self’ keyword as the first parameter (self is smth ppl use in OOP). So hit1 is thinking the plr is self, which is nil since you never initialized self in your module (once again a OOP thing). How you would make the function work with : is basically

local moves = {}
moves.__index = moves
function module:new()
   local self = setmetatable({},moves)
   return self
end
-- This is the forefront of OOP, with this functions called with : will work
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.