Can't get anything from a Module

So basically I’m trying to make a system where things fall from the sky at random positions and from random places. But, for some reason, this script I have can’t get anything from the respective settings modules that it requires.

The script:

local ServerAssets = game.ServerStorage:WaitForChild("ServerAssets")
local GlobalSettings = game.ReplicatedStorage:WaitForChild("GlobalSettings")
local GlobalValues = game.ReplicatedStorage:WaitForChild("GlobalValues")

local Functions = script.Parent.Parent:WaitForChild("Functions")
local ServerObjects = workspace:WaitForChild("ServerObjects")

local returnSumOfWeight = function(lootTable)
	local sum = 0
	for _, entry in ipairs(lootTable) do
		sum = sum + entry[1]
	end
	return sum
end

local getRandomItem = function(lootTable)
	local randomNumber = math.random(returnSumOfWeight(lootTable))

	for _, entry in ipairs(lootTable) do
		if  randomNumber <= entry[1] then
			return entry
		else
			randomNumber = randomNumber - entry[1]
		end
	end
end

while wait(GlobalValues:WaitForChild("Shards_Interval").Value) do
	local chosen = getRandomItem(require(GlobalSettings:WaitForChild("Shards_Weight")))
	local region = getRandomItem(require(GlobalSettings:WaitForChild("Shards_Regions")))
	if ServerAssets:WaitForChild("Shards"):FindFirstChild(chosen) == nil then return end
	print(region)
	local shard = ServerAssets:WaitForChild("Shards"):FindFirstChild(chosen):Clone()
	shard.Parent = ServerObjects:WaitForChild("Shards")
	shard.Position = Vector3.new(math.random(region.Min.X,region.Max.X),math.random(region.Min.Y,region.Max.Y),math.random(region.Min.Z,region.Max.Z))
	local main = require(Functions:WaitForChild("Shards"))
	main.ShardHandler(shard)
end

I’ve tried stuff like using wait() and when I printed what was the 1st value that was in a setting module, I just got “nil.” So I can’t access specific settings (i.e. Weights and Positions) as they print “nil” when I do so. No really, I have no idea how this happens. Please help!!!

May I have a look at the module script?

1 Like

This is the regions module, which holds what area it is and its random coordinates the collectibles must spawn in:

local module = {
	["Main"] = {
		Weight = 135;
		Min = Vector3.new(-1020,340,-1020);
		Max = Vector3.new(1020,345,1020);
	};
	["Marsh"] = {
		Weight = 85;
		Min = Vector3.new(1055,340,455);
		Max = Vector3.new(1990,345,455);
	};
	["Desert"] = {
		Weight = 45;
		Min = Vector3.new(-313,340,-1166);
		Max = Vector3.new(313,345,-1795);
	};
}

return module

And the weight module, which controls the weight system of the shards:

local module = {
	["Shard"] = 125;
	["TrapShard"] = 45;
	["SuperShard"] = 25;
	["VoidShard"] = 3;
	["GhostShard"] = 2;
}

return module

Which line does it fail on?
302934 char limit

The script (not the setting modules) fail when it tries to get a random weight from both modules. Not one, but the other one can error as well. As I said earlier; when I tried to get a specific argument from each of the two modules, it returns nil.

Can you paste the line where it doesn’t work?

local chosen = getRandomItem(require(GlobalSettings:WaitForChild("Shards_Weight")))

It can’t get this too:

local region = getRandomItem(require(GlobalSettings:WaitForChild("Shards_Regions")))

I think the problem is where you call entry[1]. Entry is the inner table of the module scripts, but they are defined as dictionaries so index 1 will be nil. You should be doing entry.Weight instead

I don’t see a “getrandomitem” function in your module.

Not the modules lol, the script is where it is.
To clarify:
I use modules as global Settings modules for my game so its customizable.
From inspection, it seems the script is the problem and not the module. I can’t seem to find what is the problem with the script, though.

Really? I’ll try that. I’ll update if it works or not.

Ok, turns out it doesn’t work, but I got to copy and paste the error that appears:

ServerScriptService.Shared.Shards:17: invalid argument #1 to 'random' (interval is empty) 

This is what I meant when I inspected it. It would return “nil” if I did try to get a specific argument from any of the setting modules using the script. Even while it’s clearly there it can’t seem to get it.

sry about that lol my brain is malfunctioning.

Try printing out the value of require(GlobalSettings:WaitForChild(“Shards_Weight”))

Earlier before making this post I put

print(require(GlobalSettings:WaitForChild(“Shards_Regions”))[1])

But all it returned is just “nil”, which is the problem.
This issue did spread to the “Shards_Weight” as well.

I see. This is because the module is a dictionary, so you would have to use
require(GlobalSettings:WaitForChild(“Shards_Regions”))["Main"]

Printed that, it returns “nil” still. Weird.
Everything I did should’ve made it work but for some reason doesn’t???

Sorry my bad use [“Shard”] instead of [“Main”]

Okay that works.
So I remade the structure of the modules so they actually are compatible with the script. I’ll be putting a solution now.