Why is Self.Combo returning nil?

I set Self.Combo to 1 but it returns nil in my M1 Function

local combatm1 = {}

local ActionChecking = require(game.ReplicatedStorage.Modules.Stuff.ActionChecker)
local Animations = game.ReplicatedStorage.Animations.M1s

function combatm1.new(player : Player, CData : {})
	local self = setmetatable({}, combatm1)
	
	self.Player = player
	self.Combo = 1
	self.M1Held = false

	return self
end

function combatm1:M1(player : Player, CData : {})
	self.M1Held = true
	local character = player.Character or player.CharacterAdded:Wait()
	local weapon = CData.Weapon
	local Animation = game.ReplicatedStorage.Animations.M1s[weapon]
	
	while self.M1Held do
		if character:GetAttribute("M1ing") == false then
		task.spawn(function()
			character:SetAttribute("M1ing", true)
			task.wait(.5)
			character:SetAttribute("M1ing", false)
		end)
		print(player.Name .. " Is Using " .. weapon .. " To M1!")
			local M1Animation = character:WaitForChild("Humanoid"):LoadAnimation(Animation[self.Combo])
			M1Animation:Play()
			
			if self.Combo == 4 then
				self.Combo = 1
			else
				self.Combo = self.Combo + 1
			end
		
		end
		task.wait()
	end
	
end

return combatm1

Not quite sure exactly what you mean, but your :M1 function doesn’t have any return statements. Can you send the code that handles this module or the code where this bug appears?

You’re most likely calling the method of the class on the class and not an instance of the class. Here’s an example of what that looks like:

local instance = Class.new()

Class:Method() -- NO
local instance = Class.new()

instance:Method() -- YES
1 Like

worked, but i’m wondering why I had to add this line of code to call an instance of the class since i’m still learning how to use metatables

combatm1.__index = combatm1

Ah. I hadn’t noticed your metamethod was missing. I simply assumed it was there. The following posts will elaborate on your original problem and shed light on your question:

  1. Passed parameter changes from Folder to - #8 by Ziffixture
  2. Custom Metamethods - #4 by Ziffixture