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!!!
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.
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
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.