I’m trying to set the color for different objects based on their rarity but i keep getting this error
“Unable to assign property BackgroundColor3. Color3 expected, got nil”
here is a snippet
--Colors Values
local commonColor = Color3.fromRGB(81, 81, 81)
local uncommonColor = Color3.fromRGB(47, 162, 65)
local rareColor = Color3.fromRGB(35, 185, 208)
local epicColor = Color3.fromRGB(115, 0, 172)
local legendaryColor = Color3.fromRGB(241, 133, 9)
local TextcommonColor = Color3.fromRGB(120, 120, 120)
local TextuncommonColor = Color3.fromRGB(52, 195, 57)
local TextrareColor = Color3.fromRGB(23, 255, 236)
local TextepicColor = Color3.fromRGB(170,0,255)
local TextlegendaryColor = Color3.fromRGB(241, 197, 19)
local rarityColors = {commonColor, uncommonColor, rareColor, epicColor, legendaryColor}
local rarityTextColors = {TextcommonColor, TextuncommonColor, TextrareColor, TextepicColor, TextlegendaryColor}
local rarityName = {"COMMON", "UNCOMMON", "RARE", "EPIC", "LEGENDARY"}
local function setRarity(rarity)
local color = rarityColors[rarity]
local textColor = rarityTextColors[rarity]
promptGui.Bottom.BackgroundColor3 = color
promptGui.Top.Divider.BackgroundColor3 = color
promptGui.Bottom.Rarity.TextColor3 = textColor
promptGui.Top.WeaponName.TextColor3 = textColor
promptGui.Bottom.Rarity.Text = rarityName[rarity]
end
What are you passing to setRarity when you call it? From the looks of it, you need to pass in a number from 1-5 since that’s the range of the tables you have.
You’re probably thinking you can pass in a string rarity from what I can guess, you need to use a dictionary in that case
Again most likely you’re passing in a string for setRarity judging by the name of the value, that doesn’t work since you made tables instead of dictionaries, so you need to pass in a number
A simple fix is to make the tables where the setRarity function was made into dictionaries
Example in case you may not know how dictionaries work
local rarityColors = {
Common = commonColor,
Uncommon = uncommonColor,
}
Yes, however you forgot to it for the rarityName table, and the rarityTextColors should use the same key names as rarityColors, so instead of CommonText, use Common and so on. They should all use the same names so it wont error again
The line that is causing the error is line 63 that’s the line i start changing the color
local function setRarity(rarity)
local color = rarityColors[rarity]
local textColor = rarityTextColors[rarity]
--This one promptGui.Bottom.BackgroundColor3 = color
promptGui.Top.Divider.BackgroundColor3 = color
promptGui.Bottom.Rarity.TextColor3 = textColor
promptGui.Top.WeaponName.TextColor3 = textColor
promptGui.Bottom.Rarity.Text = rarityName[rarity]
end
That means it’s still getting nil for the color and most likely the text color again, can you print the rarity in the setRarity function and tell me what it says? It’s likely the rarity you’re passing doesn’t match any of the keys in the dictionary
Im now getting this error Unable to assign property Text. string expected, got nil
local function setRarity(rarity)
local color = rarityColors[rarity]
local textColor = rarityTextColors[rarity]
promptGui.Bottom.BackgroundColor3 = color
promptGui.Top.Divider.BackgroundColor3 = color
promptGui.Bottom.Rarity.TextColor3 = textColor
promptGui.Top.WeaponName.TextColor3 = textColor
--On this line promptGui.Bottom.Rarity.Text = rarityName[rarity]
end