So i made a table, and im trying to make it so depending on the rarity the template background color would change, so if it was legendary the background color would be yellow. But im not sure why it isn’t working, when i get something other than legendary the background color would turn to yellow even though the item wasn’t legendary
for i, v in pairs(ArmorModule.rarities) do
if ArmorModule.armors["Legendary"] then
newTemplate2.VP.BackgroundColor3 = Color3.new(1, 0.606363, 0)
newTemplate.VP.BackgroundColor3 = Color3.new(1, 0.67776, 0)
elseif ArmorModule.armor["Epic"] then
newTemplate2.VP.BackgroundColor3 = Color3.new(0.688365, 0.250324, 0.985275)
newTemplate.VP.BackgroundColor3 = Color3.new(0.688365, 0.250324, 0.985275)
end
end
This doesn’t appear to be checking if “Legendary” was selected, but rather if “Legendary” exists at all.
I don’t have enough code to work with to know what line of code to type out for you to replace it with, but this could be the source of the problem. I hope this helps!
Side note: This isn’t important, but you may find it beneficial. It looks a little tedious to define colors using very specific numbers like 0.606363; you may prefer to use Color3.fromRGB() which takes a number from 0 to 255 instead of from 0 to 1.
local armormodule = {}
armormodule.armors = {
["Legendary"] = {
game.ReplicatedStorage.Armors.LegendaryArmor
};
["Epic"] = {
game.ReplicatedStorage.Armors.EpicArmor
}
}
armormodule.rarities = {
["Legendary"] = 1; -- 0.001% chance
["Epic"] = 5; --10% chance
}
local sum = 0
for i, v in pairs(armormodule.rarities) do
sum += v
end
armormodule.chooseRandom = function()
local randomNumber = math.random()*sum
local counter = 0
for rarity, weight in pairs(armormodule.rarities) do
counter = counter + weight
if randomNumber <= counter then
local rarityTable = armormodule.armors[rarity]
local chosenArmor = rarityTable[math.random(1,#rarityTable)]
return chosenArmor
end
end
end
return armormodule
Alright try this for the for loop in the first script,
for i, v in pairs(ArmorModule.rarities) do
if table.find(ArmorModule.armors["Legendary"], newArmor) then
newTemplate2.VP.BackgroundColor3 = Color3.fromRGB(236, 127, 0)
newTemplate.VP.BackgroundColor3 = Color3.fromRGB(236, 127, 0)
elseif table.find(ArmorModule.armor["Epic"], newArmor) then
newTemplate2.VP.BackgroundColor3 = Color3.fromRGB(138, 97, 255)
newTemplate.VP.BackgroundColor3 = Color3.fromRGB(138, 97, 255)
end
end
Replace this line:
local newArmor = ArmorModule.chooseRandom():Clone()
Replace that line with the following:
local newRandomArmor = ArmorModule.chooseRandom()
local newArmor = newRandomArmor:Clone()
And change the for loop I gave you to this:
for i, v in pairs(ArmorModule.rarities) do
if table.find(ArmorModule.armors["Legendary"], newRandomArmor) then
newTemplate2.VP.BackgroundColor3 = Color3.fromRGB(236, 127, 0)
newTemplate.VP.BackgroundColor3 = Color3.fromRGB(236, 127, 0)
elseif table.find(ArmorModule.armor["Epic"], newRandomArmor) then
newTemplate2.VP.BackgroundColor3 = Color3.fromRGB(138, 97, 255)
newTemplate.VP.BackgroundColor3 = Color3.fromRGB(138, 97, 255)
end
end