Tables in my module always print nil

This code is sort of in progress but working on fixes for a few bugs revealed that whenever I try to use a table such as enemyTable (I tried creating a getEnemyObject() which returned the index of the table) it prints as nil

local EnemyModule = {}
EnemyModule.__index = EnemyModule

EnemyModule.enemyTable = {}

function returnEnemyTable()
	print(EnemyModule.enemyTable)
	return EnemyModule.enemyTable
end


task.spawn(function()
	--while true do
	--	task.wait(1)
	--	print(returnEnemyTable())
	--end
end)

-- Helper Functions
function calculateInitialSize()
	local sizeValue = math.random(1, 100) -- Just to determine whether it will be a small or big sized enemy
	local actualSize = nil

	if sizeValue >= 90 then
		actualSize = math.random(10, 20)
	else
		actualSize = math.random(1, 5)
	end
	return actualSize
end

function shutDown(self)
	self.Model:Destroy()
end

function updateSize(self, amount)
	self.Size += amount

	local size = self.Size
	print("updateSize called, setting Head.Size to", size)
	self.Model.Head.Size = Vector3.new(size, size, size)

	if self.Size >= self.MaxSize then
		shutDown(self)
	end
end


ingredientProperties = {
	["Blueberry"] = {
		["Name"] = "Blueberry",
		["Model"] = game.ReplicatedStorage.EnemyModels.Blueberry,
	}
}

function getProperties(type)
	print(type)
		print(ingredientProperties[type]) --prints NIL
	return ingredientProperties[type]
end

function EnemyModule.new(type)
	local blueberryModel = game.ReplicatedStorage.EnemyModels.Blueberry:Clone()
	blueberryModel.Parent = workspace

	local sizeValue = calculateInitialSize()
	local MaxSizeValue = sizeValue + math.random(1, 10)
	
	local sizeInt = blueberryModel.Size


local newEnemy = setmetatable({
		Type = type,
		unpack(getProperties(type)), -- is Nil
		Size = sizeValue,
		MaxSize = MaxSizeValue
}, EnemyModule)

	blueberryModel.Head.Size = Vector3.new(newEnemy.Size, newEnemy.Size, newEnemy.Size)
	
	sizeInt.Value = newEnemy.Size
	

	EnemyModule.enemyTable[blueberryModel] = newEnemy

	return newEnemy
end


function EnemyModule.GetEnemyFromCharacter(char)
	print(EnemyModule.enemyTable) -- Prints nil
	return returnEnemyTable()
end

function EnemyModule:UpdateSize(amount)
	updateSize(self, amount)
end

function EnemyModule:ShutDown()
	shutDown(self)
end

return EnemyModule

I put this in the command bar, and everything seemed to work just fine, nothing printed nil:

local m = require(workspace.ModuleScript)
m.new("Blueberry")
print(m.GetEnemyFromCharacter(workspace.Blueberry))
1 Like

I wonder if there’s something wrong with my game or computer because I’m also having another problem where the code seems perfectly fine but its not working. I don’t have any scripts that interfere with it but at least I know the code is supposed to work.

If the modules look fine, the problem could be how you’re calling or using the modules.

1 Like

yes make sure your calling it with require() and in a script or local script and if your trying to call a specific function use ModuleScript.Function() of course replace function with function name and modulescript with module name

1 Like