Cannot change the background of a frame inside a script

Hello, Im trying to make it so a frame’s colour is changed to a colour inside a module script. But I’m getting an error.
Error:

Script:

local Player = game.Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local ReplicatedFirst = game:GetService("ReplicatedFirst")

local InventoryGui = script.Parent
local Container = InventoryGui:WaitForChild("InventoryStorage")

local ItemProps = require(ReplicatedStorage.Modules.ItemProperties)
local RarityColours = require(ReplicatedStorage.Modules.ItemProperties.RarityColours)

local DataFolder = ReplicatedStorage.ReplicatedData:WaitForChild(Player.UserId)

DataFolder.Inventory.Changed:Connect(function(encodedData)
	local dataTable = HttpService:JSONDecode(encodedData)
	for ItemName, amount in pairs(dataTable) do
		local ItemFrame = Container:FindFirstChild(ItemName)

		if (not ItemFrame) then
			ItemFrame = ReplicatedStorage.ItemFrame:Clone()
			ItemFrame.Parent = Container
			ItemFrame.Name = ItemName
			ItemFrame.Image = ItemProps[ItemName].ImageId
			local RarityItem = RarityColours[ItemFrame.Name].Rarity
			ItemFrame.ColourRarity.BackgroundColor3 = (RarityColours[RarityItem])
		end

		ItemFrame.AmountLabel.Text = "x"..amount

		if amount <= 0 then
			ItemFrame:Destroy()
		end
	end
end)

lines that are causing the error:

local RarityItem = RarityColours[ItemFrame.Name].Rarity
ItemFrame.ColourRarity.BackgroundColor3 = (RarityColours[RarityItem])

Module script:

local RarityColours = {
	["Common"] = Color3.fromRGB(255,255,255),
	["Uncommon"] = Color3.fromRGB(69, 205, 255),
	["Rare"] = Color3.fromRGB(60, 255, 66),
	["Legendary"] = Color3.fromRGB(255, 32, 36),
	["Mythic"] = Color3.fromRGB(162, 51, 190)
}

return RarityColours
local RarityItem = RarityColours[ItemFrame.Name]
ItemFrame.ColourRarity.BackgroundColor3 = RarityItem

see how the error has “nil” in it, it means that something doesn’t exist and is related with “Rarity”

Try checking if theRarityColours[ItemFrame.Name] exist before indexing it with Rarity, so this statement would be like:

if not ItemFrame and RarityColours[ItemFrame.Name] then
    ...
end

I have found the solution, I was a sleepy head this morning and I forgot I had another module script that can get the rarity of the item via the item name:

Solution:

local ItemRarity = ItemProps[ItemName].Rarity
local RarityItem = RarityColours[ItemRarity]
ItemFrame.ColourRarity.BackgroundColor3 = RarityItem

“ItemProps” being item properties

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.