You can make a dictionary with different values inside of them, sort of like making simulator eggs and hatching a variety of pets:
local Items = {
BadGun = {Name = "BadGun", Rarity = 100}, -- The higher the rarity, the more it spawns
GoodGun = {Name = "BadGun", Rarity = 10},
BestGun = {Name = "BadGun", Rarity = 1}
}
You would then loop through and add up all the rarity values
local UniversalRarity = 0
for i, v in pairs(Items) do
UniversalRarity = UniversalRarity + v.Rarity
end
Then you would assign locations on where the guns would spawn and loop through all the locations:
local Locations = -- Table of all the locations where the guns will spawn, or random locations
local UniversalRaritySubtract = UniversalRarity -- Were cloning the value, because this one will be changed
local Index = 1
for i, Location in pairs(Locations) do
local Chance = math.random(1, UniversalRarity)
UniversalRaritySubtract = UniversalRaritySubtract - Chance
Index = Index + 1
if UniversalRaritySubtract <= Items[Index].Rarity then
print(Items[Index].Name) -- prints out the chosen gun
end
end
The final code would look something like this:
local Items = {
BadGun = {Name = "BadGun", Rarity = 100},
GoodGun = {Name = "BadGun", Rarity = 10},
BestGun = {Name = "BadGun", Rarity = 1}
}
local UniversalRarity = 0
for i, v in pairs(Items) do
UniversalRarity = UniversalRarity + v.Rarity
end
local Locations = -- Table of all the locations where the guns will spawn, or random locations
local UniversalRaritySubtract = UniversalRaritychanged
local Index = 1
for i, Location in pairs(Locations) do
local Chance = math.random(1, UniversalRarity)
UniversalRaritySubtract = UniversalRaritySubtract - Chance
Index = Index + 1
if UniversalRaritySubtract <= Items[Index].Rarity then
print(Items[Index].Name)
end
end