Help with randomly generating a sequence of weapons


So what am I trying to do?
To simplify, I have a folder in server storage, and in side it has folders with weapons in each.
My game is a round base game and it chooses weapons from the folders and gives them, my problem with this is that, the same weapon theme keeps coming.

Part of my Script:

local WepTheme = game.ServerStorage.Weapons:GetChildren()
local ChosenWepTheme = WepTheme[math.random(1,#WepTheme)]
local weps = ChosenWepTheme:GetChildren()

I want to change it so the game makes a sequence beforehand, for example Sword, slingshot, gun and once all 3 rounds are over it generates a new sequence.


Can you show a picture of server storage, and print the results of ChosenWepTheme?

1 Like

Here is the picture:

and for print result:
I did

local WepTheme = game.ServerStorage.Weapons:GetChildren()
local ChosenWepTheme = WepTheme[math.random(1,#WepTheme)]
local weps = ChosenWepTheme:GetChildren()

Attempt1: Classic Sword
Attempt2: Gun
Attempt3 Classic Sword
Attempt4: slingshot

Make a table with all the weapons that have already been used like this:

local usedWeps = {}

local WepTheme = game.ServerStorage.Weapons:GetChildren()

    local ChosenWepTheme = WepTheme[math.random(1,#WepTheme)] 
until not ChosenWepTheme in usedWeps

table.append(usedWeps, ChosenWepTheme)

local weps = ChosenWepTheme:GetChildren()

Sorry if this is wrong, I am on mobile do it is hard to type it out

I have a better verison, my last one created a table and added all used weapons to it, but I think this is more efficient:

local WepTheme = game.ServerStorage.Weapons:GetChildren()

randNum = math.random(1, #WepTheme)
local ChosenWepTheme = WepTheme[randNum] 
table.remove(WepTheme, randNum)

local weps = ChosenWepTheme:GetChildren()

This version removes the chosen weapon folder from the list of available weapons, so that it will not be found again. I hope this has no errors, sorry if it does!


The way it is currently set up, you have 3 themes, sword, gun, slingshot, and one weapon in each theme.
You have the name of the themes the same as the name of the items, so you are actually printing the themes instead of the actual item. That is why it prints “Classic Sword” (The name of the theme) instead of “Sword” (the name of the weapon). Everything looks to be working as intended, but I would rename the themes(folders) and put more tools in each and you will see they are random.

Then you can iterate over your randomly selected theme (Group of weapons) with a for loop to put them in Backpack/StarterGear.

for i, v in pairs(ChosenWepTheme) do
     local weapon = v:Clone()
     --parent the weapon where you would like it

I did some testing with this script

For some reason it keeps skipping a weapon

for example: sword, slingshot, bomb, then slingshot, sword, gun

I am honestly really confused why it did that

(yes, I added another theme)

I will keep testing to see what happens.

EDIT: I did more testing and in one of my attempts
I got sword, sword, sword

my theory: even tho you remove the weapon from the table, the table gets reset every round because we do

U can do this:

  1. Name the weapons with numbers

  2. Put this script and modify it!

local RandomGun = math.random(1, 10)

local gun = game.ReplicatedStorage.Weapons[RandomGun]

-- Put here the gun into inventory!

We are trying to make it so no weapon is repeated but thanks anyway

Many people is having problems like this but all is for lua!, i dont know because, very rare errors…

Oh sorry I didn’t think about that. I guess you can just not reset the list of things each time, and then reset it at the end. Or go with the first thing I sent and only reset the table at the end of whatever

Every time this line runs, you are going to refresh your table. That is why removing them does nothing.

local WepTheme = game.ServerStorage.Weapons:GetChildren()

You need to have this outside your game loop, or at least outside the scope of the rounds until you want all the weapon choices to reset.

1 Like

Yeah, that’s exactly what I said

This my version (I think it is more reliable). Same plan: remove chosen weapon from array.

-- Weapons themes helping.

local weaponThemes = game.ServerStorage.Weapons:GetChildren()

local function chooseWeaponTheme()
    local randomIndex = math.random(1, #weaponThemes)
    local chosenWeaponTheme = weaponThemes[randomIndex]

    if not chosenWeaponTheme then
        -- no available weapon (all the weapons have been removed since they were used)
        -- restore array of weaponThemes
        weaponThemes = game.ServerStorage.Weapons:GetChildren()
        -- recursive to get the chosenWeaponTheme this time.
        return chooseWeaponTheme()

    -- remove the chosenWeaponTheme from the array (so it wouldn't be chosen next time if there are other weapon themes)
    table.remove(weaponThemes, randomIndex)

    return chosenWeaponTheme

local weaponTheme = chooseWeaponTheme()
1 Like

thanks, I will do some testing

ServerScriptService.MainScript:57: invalid argument #2 to 'random' (interval is empty)

It seems that it is not finding any weapons in the location. Try print(weaponThemes) and print(#weaponThemes) and see what it gives you. You might have the wrong path typed in or something.

basically after i remove from the table the table is empty.

This line does not run
I tried

if #weaponThemes == 0 then
      weaponThemes = game.ServerStorage.Weapons:GetChildren()

I tried checking if the folder is empty but for some reason the if statement does not run.
resulting in the error

Hmm. I wonder if it would work if you made it if #chosenWeaponTheme == 0. Maybe its because chosenWeaponTheme still exists (and is a list, not nil), but just is empty. Not sure tho.

I think it might be a roblox issue?