GetChildren in a module?

I don’t know the best way to explain this, but what I am trying to do is a randomized gun spawner, and it’ll spawn a randomized rarity of that gun. I have a module for just the rarities for the guns, and here is an example;

  ['Assault Rifle'] = {
    "Rare",
    "Uncommon",
    "Common",
  };

If I wanted to math.random to get a randomized rarity, how would this be achieved, in a module?

You would use math.random on your table, for example:

YourTable[math.random(1, #YourTable)])

YourTable will be that table that you would be using. You will have to require the module first though of course to get the table in the first place.

If you want a randomized rarity in a module you could do this and edit how you please
local module = {}
local Chance = math.random(100)
local Rarity =

function module.Randomize()
if Chance == 1 then
Rarity = "Rare" 1% Chance
elseif Chance >= 20 then
Rarity = "Uncommon" -- 20 % Chance
elseif Chance >= 50 then
Rarity = "Common" -- 50% chance
end
return Rarity end

return module
1 Like

No no no, they are DIFFERENT weapons, in the module, those weapons are randomized with a server side script, it randomizes and choses a weapon, and then checks the Rarity module for that gun for the rarity, not a bunch of Elseifs. I am trying to figure out to randomize its rarity, of it being part of the module it required.

To reply to this here is my code so far;

--Setup--
local RepStorage = game:GetService("ReplicatedStorage")
local RaritySettings = require(RepStorage.GunsSpawnInc.Settings)
local GunDetails = require(RepStorage['Guns.Config'].GunDetails)
local GunRarities = require(RepStorage['Guns.Config'].Rarities)
local GunRaritiesReC = require(RepStorage['Guns.Config'].RaritiesReCong)
function randomizeweapon()
  local Guns = game.ReplicatedStorage.Guns:GetChildren()
  local GunsRandom = Guns[math.random(1, #Guns)]
  return GunsRandom
end
--Maths--
--local tr = randomizeweapon()
local Guns = game.ReplicatedStorage.Guns:GetChildren()
local GunsRandom = Guns[math.random(1, #Guns)]
if GunsRandom:IsA('Model') then
  local RarityRandom = GunRarities[GunsRandom]
  local mathsforrare = RarityRandom[math.random(1, #RarityRandom)]
  local FindRares = GunRaritiesReC[mathsforrare]
  local tlc = game.ReplicatedStorage.GunsSpawnInc.GunLight:Clone()
  local tle = game.ReplicatedStorage.GunsSpawnInc.Emmit:Clone()
  tlc.Color = FindRares
  tle.Color = FindRares
  local r = GunsRandom:Clone()
  r.Parent = workspace
  tlc.Parent = r
  tle.Parent = r
end
	
	

You can see in line 18 I attempted @3rdhoan123 's solution, and this has not worked.

For the edit;

Ignore the function and --local tr = randomizeweapon()
this is just me testing something else, and this is no where near my issue, as the error points at line 18, which again, is the answer 3rd provided me with

Don’t do getchildren, you want to get a randomized rarity of a gun from a table right?
Require the module that has the rarities, then do:
local Guns = require(game.ReplicatedStorage.GunRarities) – Basicaly just require your table of rarities.
To require the table
Next,
You want to randomize it via using the math.random on the table.

GetChildren() is for the GUNS. Meaning the gun MODELS. Nothing to do with rarities.

What happens when you ran the script. I’m not too sure if there are any problems as I can see so far.

Ah, I see where you randomized the rarities though, local RarityRandom has to be
you require()'ing the table

Please, for the second time, read before you post. I clearly require() the GunRarities in line 5.

He wants to randomize based on the number of guns in the place he put it in.

1 Like

I don’t think we are on the same page, lets just deconstruct it.

local GunRarities = require(RepStorage['Guns.Config'].Rarities)
local RarityRandom = GunRarities[GunsRandom] --the GUN NAME being found in the MODULE the RANDOM gun CHOSEN
local mathsforrare = RarityRandom[math.random(1, #RarityRandom)] --CHOSING the rarities available under that guns NAME in the module
local FindRares = GunRaritiesReC[mathsforrare] --Getting the chosen rarities color

Here is an example of what a rarity looks like;

  ['Assault Rifle'] = {
    "Rare",
    "Uncommon",
    "Common",
  };

Example of a rarity in the module for the colors;

  ['Uncommon'] = Color3.new(17, 255, 0),

Your code is still erroring, and here is the error, still is the same thing, line 18, error;

[attempt to get length of a nil value]

Guns random seems to be a Model on the if statement, then you specify in GunRarities[GunsRandom] as GunsRandom as a number value. (If that makes sence)

When you specified it in line 18, it was nil. I think.

Oh! My bad, forgot to add GunsRandom.Name, lets see if that works

It’s no longer nil, if I print before line 18, print(GunsRandom.Name) it’ll print the name. But the same error happens for line 18.

Could you show the rarities module script? You seem to be confusing a number and name values.

I fixed it, I just named some things the wrong way, that was the issue the whole time.

2 Likes

Ah good. What did you name wrong? (Just Wondering.)

Every gun, lol, I had so many weapons in the folders and I named them all differently.