Module Script Help

So I made a module script to store types of materials

Module

local Materials = {

Enum.Material.Grass,

Enum.Material.WoodPlanks,

Enum.Material.Cobblestone,

}

return Materials

and I made a script to run it that picks a random material out the list

Script

local config = require(game.ReplicatedStorage.Modules.Materials)

local RandomMaterial = config.Materials[math.random(1, #config.Materials)]

print(RandomMaterial)

comes out with “attempt to get length of a nil value”

What did I do wrong?

This is because the module returns a table that contains all the materials, yet you are trying to index that table with Materials. You should just use #config instead of #config.Materials and that should fix it.

I plan on having multiple tables under one module script so how would I reference the “Materials” table directly under that module script?

1 Like

You will have to have it return a table that contains all the tables you want to use.
Example (this would replace the last line of the module script):

return {
	["Materials"] = Materials,
}
1 Like