Attempt to call a nil value : on required module

I have coded some q-learning, and when i tryed to start enviroment and print it i got an error…

Here is script code:

local Env = require(script['Enviroment'])
local Actor = require(script['Actor'])
local Agent = require(script['Agent'])

w = Env.new(7)
w:play() -- here error
w:print()

Here is enviroment full code:

Code
local Enviroment = {}

local Actor = require(script['Parent']['Actor'])
local Agent = require(script['Parent']['Agent'])
local Enemy = require(script['Parent']['Enemy'])

function Enviroment.new(n)
	
	local self = setmetatable({}, Enviroment)
	self.n = n
	self.P = Agent.new(Actor, 1, 1, self.n)
	self.ens = {Enemy.new(Actor,3,3,self.n), Enemy.new(Actor,4,4,self.n), Enemy.new(Actor,5,5,self.n)}
	
	return self
end

function Enviroment:step()
	for i, en in pairs(self.ens) do
		en:move()
	end
	self.P:move()
end

function Enviroment:print()
	for i=1, 100 do print("\n") end
	local pvector = self.P:getXY()
	
	local wmap = {}
	
	for j=1, self.n do
		wmap[j] = {}
		for i=1, self.n do
			wmap[j][i] = 0
		end
	end
	
	self.wmap = wmap
	
	self.wmap[pvector.x][pvector.y] = 1
	for i, en in pairs(self.ens) do
		local evector = en:getXY()
		self.wmap[evector.x][evector.y] = 2
	end
	for i, v in pairs(self.wmap) do
		print(v)
	end
end

function Enviroment:play()
	local pvector = self.P:getXY()
	local bl = true
	for i, en in pairs(self.ens) do
		local evector = en:getXY()
		bl = bl and pvector ~= evector
	end
	local iter = 0
	while bl do
		wait(1)
		self:print()
		self:step()
		local pvector = self.P:getXY()
		bl = true
		for i, en in pairs(self.ens) do
			local evector = en:getXY()
			bl = bl and pvector ~= evector
			print(pvector, " | ", evector)
		end
		print("______")
		iter = iter + 1
	end
	print(iter)
end

return Enviroment

Anybody can help me?
I think error in function play, in while loop, but maybe not

Try

local self = setmetatable({}, {__index = Enviroment})

When __index of the MT is set to a table, any lookup into self that would return nil looks inside the __index table instead.

Thanks, but this didn’t help
(thirtycharacteeers)

1 Like

Here’s a super basic example that shows the general setup.

local Enviroment = {}

function Enviroment.new(n)
	local self = setmetatable({}, {__index = Enviroment})
	
	return self
end

function Enviroment:play()
	print("It works!")
end

return Enviroment
local M = require(script.ModuleScript)
local a = M.new()
a:play()

I tested this and it works as expected

Damn, i think i got solution, i have Enemy module without __index and it make error

1 Like