Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ModuleScript table is messing up again?

local MaterialArray = require(game.ServerStorage.materials)

function dictLen(dict)
    local x = 0
    table.foreach(dict, function() x = x + 1 end)
    return x
end

while true do
	wait()
	local Material = math.random(1, dictLen(MaterialArray))
	if MaterialArray[Material][3][script.Parent.Floor.Value] == true then
		script.Parent.BrickColor = Material[2]
		script.Parent.Material = Material[1]
		script.Parent.Material.Value = Material.Name
	end
end

Is giving me the following error: Workspace.OreTest.LoadScript:12: attempt to index field '?' (a nil value)

Module Script:

materials = {
--	[Material Name[Sting]] = {Texture[Enum], Color[BrickColor], {B[Bool], 1[Bool], 2[Bool], 3[Bool], 4[Bool], 5[Bool]}, BasePrice[Int], Tools[Bool], Duribilaty[Int], Speed[Int]},

	-- Buyable Materials
	["Wood"] = {Enum.Material.Wood, BrickColor.new("Cork"), {false, false, false, false, false, false}, 1, true, 10, 10},
	
	-- Mineable Materials
	["Coal"] = {Enum.Material.Slate, BrickColor.new("Black"), {true, true, true, true, false, false}, 2, false, 0, 0},
	["Stone"] = {Enum.Material.Slate, BrickColor.new("Smoky grey"), {true, true, true, true, true, true}, 1, true, 15, 9},
	["Iron Ore"] = {Enum.Material.Marble, BrickColor.new("Medium stone grey"), {true, true, true, true, true, false}, 2, false, 0, 0},
	["Copper Ore"] = {Enum.Material.Marble, BrickColor.new("Bright orange"), {false, true, true, true, false, false}, 1, false, 0, 0},
	["Diamond"] = {Enum.Material.Marble, BrickColor.new("Deep blue"), {false, false, false, false, true, true}, 20, false, 0, 0},
	
	-- Createable Materials
	["Iron"] = {Enum.Material.Slate, BrickColor.new("Medium stone grey"), {false, false, false, false, false, false}, 8, true, 15, 8},
	["Copper"] = {Enum.Material.Marble, BrickColor.new("Bright orange"), {false, false, false, false, false, false}, 4, true, 12, 9},
	["Steel"] = {Enum.Material.Marble, BrickColor.new("Medium stone grey"), {false, false, false, false, false, false}, 10, true, 30, 5},
	["Diamond coated Steel"] = {Enum.Material.Marble, BrickColor.new("Deep blue"), {false, false, false, false, true, true}, 30, true, 100, 4},
	
	-- Intermediate Materials (Materials made by crafting that are used for crafting)
	["Carbon"] = {Enum.Material.SmoothPlastic, BrickColor.Random(), {false, false, false, false, false, false}, 6, false, 0, 0},
	
	-- Special (Events / Codes / etc.)
	["Roblox / AlphaBlox Red"] = {Enum.Material.SmoothPlastic, BrickColor.new("Bright Red"),{false, false, false, false, false, false}, 0, true, math.huge, 10},
	["Roblox / Twitter White"] = {Enum.Material.SmoothPlastic, BrickColor.new("White"),{false, false, false, false, false, false}, 0, true, math.huge, 10},
	["Twitter Blue"] = {Enum.Material.SmoothPlastic, BrickColor.new("White"),{false, false, false, false, false, false},  0, true, math.huge, 10},
	["AlphaBlox Black"] = {Enum.Material.SmoothPlastic, BrickColor.new("Black"),{false, false, false, false, false, false},  0, true, math.huge, 10},
}

return materials

I would say more, but that is all I know.

2 answers

✓ Accepted answer
local Material = math.random(1, dictLen(MaterialArray))

local i = 0
local Key 

for key, value in pairs(MaterialArray) do
	i = i + 1
	if i == Material then
		Key = key
		break
	end
end

while wait() do
	if Key then
		if MaterialArray[Key][3][script.Parent.Floor.Value] == true then
			--Do your stuff
		end
	end
end

A quick code I wrote up, mess around with it

Theoritically, the error happens when you try to fetch a part of a nil table.... or in other words not defined.

Which goes back to line 12.

   if MaterialArray[Material][3][script.Parent.Floor.Value] == true then

and then line 11

  local Material = math.random(1, dictLen(MaterialArray))

Which tells that you were trying to go for things such as MaterialArray[(numbers)] instead of the actual key of the Arrays. Which are actually strings. So a way to solve the issue would be to instead make another table that holds number keys that corresponds to string keys.

Or Basically.

local MaterialArray = require(game.ServerStorage.materials)
local keyArray = {}

for k,_ in pairs(MaterialArray) do
  table.insert(keyArray, k)
end

function dictLen(dict)
    local x = 0
    table.foreach(dict, function() x = x + 1 end)
    return x
end

while true do
    wait()
    local Material = math.random(1, dictLen(MaterialArray))
    local mat = keyArray[Material]
    if MaterialArray[mat][3][script.Parent.Floor.Value] == true then
        script.Parent.BrickColor = Material[2]
        script.Parent.Material = Material[1]
        script.Parent.Material.Value = Material.Name
    end
end

(Untested)

But should work.

This discussion is preserved as it was. New answers, comments, and votes are closed.