Incorrect color with rarity

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

Armor Table

armormodule.armors = {
	
	["Legendary"] = {
		game.ReplicatedStorage.Armors.LegendaryArmor
	};

	["Epic"] = {
		game.ReplicatedStorage.Armors.EpicArmor
	}

}

Can we see more of your scripts, like how you are defining “newTemplate2”, and the ArmorModule.rarities table.

1 Like

I think the problem might be with

if ArmorModule.armors["Legendary"] then

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.

1 Like

sorry to keep you waiting
Heres the full script

local replicatedstorage = game:GetService("ReplicatedStorage")
local ArmorModule = require(game.ReplicatedStorage:WaitForChild("ArmorHandler"))
local remote = replicatedstorage.Events:WaitForChild("UpdateInventory")
local newArmor = ArmorModule.chooseRandom():Clone()
local DataUpdater = game.ReplicatedStorage.Events:WaitForChild("DataUpdater")

replicatedstorage.Events.UpdateInventory.OnClientEvent:Connect(function(player, armor)
	local template = script.Parent.MainGui.InventoryHolder.Inventory.Templates.Template
	local newTemplate = template:Clone()
	newTemplate.Parent = script.Parent.MainGui.InventoryHolder.Inventory.Duplicates


			
	local camera = Instance.new("Camera")
	local armorname = newArmor.Name
	local template2 = script.Parent.MainGui.Reward.Template
	local newTemplate2 = template2:Clone()
	newTemplate2.Parent =  script.Parent.MainGui.Reward
	
	camera.CFrame = CFrame.new(newArmor.PrimaryPart.Position + (newArmor.PrimaryPart.CFrame.lookVector * 3), newArmor.PrimaryPart.Position)
	camera.Parent = newTemplate2.VP
	newArmor.Parent = newTemplate2.VP
	newTemplate2.VP.CurrentCamera = camera
	script.Parent.MainGui.Reward.Visible = true
	newTemplate2.Visible = true
	wait(2)
	script.Parent.MainGui.Reward.Visible = false
	newTemplate2.Visible = false
	newTemplate.Visible = true
	
	camera.CFrame = CFrame.new(newArmor.PrimaryPart.Position + (newArmor.PrimaryPart.CFrame.lookVector * 3), newArmor.PrimaryPart.Position)
	camera.Parent = newTemplate.VP
	newArmor.Parent = newTemplate.VP
	newTemplate.VP.CurrentCamera = camera
	newTemplate:WaitForChild("ArmorName").Value = armorname
	newTemplate.Name = armorname
	DataUpdater:FireServer(player, armor)
	
	for i, v in pairs(ArmorModule.rarities) do
		if ArmorModule.armors["Legendary"] then
			newTemplate2.VP.BackgroundColor3 = Color3.fromRGB(236, 127, 0)
			newTemplate.VP.BackgroundColor3 = Color3.fromRGB(236, 127, 0)
		elseif ArmorModule.armor["Epic"] then
			newTemplate2.VP.BackgroundColor3 = Color3.fromRGB(138, 97, 255)
			newTemplate.VP.BackgroundColor3 = Color3.fromRGB(138, 97, 255)
		end
	end
end)


im not sure what you mean?

Can we also see the ArmorModule, I think I know how to fix it but I need to see the module script.

Here

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

What is “armorname” equal to and what is “armor” equal to?

armorname is = newarmor.name and newarmor is the chosen armor
armor is = to newarmor

local armorname = newarmor.name

local armor = newArmor

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

should i do anything else?

the colors don’t change?

Can you do this:

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
1 Like

Yes, thanks it works now

1 Like