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.
local WepTheme = game.ServerStorage.Weapons:GetChildren()
local ChosenWepTheme = WepTheme[math.random(1,#WepTheme)]
local weps = ChosenWepTheme:GetChildren()
print(ChosenWepTheme)
Make a table with all the weapons that have already been used like this:
local usedWeps = {}
local WepTheme = game.ServerStorage.Weapons:GetChildren()
repeat
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
end
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
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()
end
-- 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
end
local weaponTheme = chooseWeaponTheme()
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.
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.