Attempt to call a nil value when calling function in module

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Powers = {}
Powers.__Index = Powers

function Powers.new(Name,Color,IsPassive,effect,key)
	
	local self = setmetatable({}, Powers)
	
	self.Name = Name or "Power"
	self.Color = Color or Color3.new(1,1,1)
	self.IsPassive =  IsPassive
	
	if self.IsPassive == false then
		self.Key = key or Enum.KeyCode.RightControl
	end
	
	self.Effect = effect or "Pending"
	
	self.Object = ReplicatedStorage.Orb:Clone()
	self.Dependencies = "Pending"
	
	self.Object.Outside.Color = self.Color
	self.Object.Inside.Color = self.Color
	
	self.Object.GetOrbData.OnInvoke = function()
		return self
	end
	
	self.Object.ProximityPrompt.Triggered:Connect(function(Player)
		print(getmetatable(self) , Powers)
		print(self:Add(Player))
	end)
	
	return self
	
end

function Powers:Add(Player)
	local playerModule = require(game.ServerStorage.PowerModules[Player.UserId])
	local playerModule2 = require(Player.Character.PowersModule)
	if not playerModule then error("PowerModule not found") end
	playerModule.Powerlist[self.Key] = self.Effect
	playerModule2.Powerlist[self.Key] = self.Effect
	self.Object:Destroy()
	return "Success"
end 



return Powers

When self:Add() is called i get the attempt to call a nil value, despite it being right there.

1 Like

In this case “self” is a reference to the instance through which/on which the instance method is called, the variable named “Powers” which itself is a reference/pointer to some table value.

1 Like

Yes, but my this point the table’s metatable has already been set.

1 Like

Oops, I just realised i wrote Powers.__Index instead of Powers.__index

Now I get this error though

ReplicatedStorage.PowerClassHandler:44: attempt to index nil with EnumItem
From this line

	print(self.Key)
	playerModule.Powerlist[self.Key] = self.Effect

self.Key in this case is Enum.KeyCode.R

I was just providing more clarification regarding the way in which the method is defined, in case the issue stems from the script(s) which is/are requiring this module script.

That means playerModule.Powerlist must be nil.

I see, I always fail to understand that error despite being fairly straight forward for some reason :stuck_out_tongue: