Code issues with table list and frames

Hello, how can I make each white box (frame) the index of a list and the colored parts (red, green and blue) the objects to be added to that list?

For example, If I add the blue part, frame number 1 appears blue, if I make an ‘insert’ with the red part, the blue box goes to position 2 and the red part is in position one, is this possible? how can I do it?

image

localscript:

-- items
local it1 = script.Parent.item1
local it2 = script.Parent.item2
local it3 = script.Parent.item3

-- Touches parts
local tpart1 = game.Workspace.Touching1 -- RED
local tpart2 = game.Workspace.Touching2 -- BLUE
local tpart3 = game.Workspace.Touching3 -- GREEN

-- Debounce system
local Debounce = false

-- tables
local obj = {}

-- touches
tpart1.Touched:Connect(function(hit)
	if not Debounce then -- dobounce
		if hit.Parent:FindFirstChild("Humanoid") then
			Debounce = true
			table.insert(obj,1,it1)
			print("touches red part")
			wait(5)
			it1.BackgroundColor3 = tpart1.Color
			wait(5)
			print(obj)
			Debounce = false
			wait(2)
			print(#obj) -- CONTADOR - CANTIDAD DE COSAS EN LA LISTA
		end
	end -- end debounce
end)

tpart2.Touched:Connect(function(hit)
	if not Debounce then -- dobounce
		if hit.Parent:FindFirstChild("Humanoid") then
			Debounce = true
			table.insert(obj,1,it2)
			print("touches blue part")
			wait(5)
			it2.BackgroundColor3 = tpart2.Color
			wait(5)
			print(obj)
			Debounce = false
		end
	end -- end debounce
end)

tpart3.Touched:Connect(function(hit)
	if not Debounce then -- dobounce
		if hit.Parent:FindFirstChild("Humanoid") then
			Debounce = true
			table.insert(obj,1,it3)
			print("touches green part")
			wait(5)
			it3.BackgroundColor3 = tpart3.Color
			wait(5)
			print(obj)
			Debounce = false
		end
	end -- end debounce
end)

You could create a table for the objects and loop through them as opposed to making separate touched events.


-- Touches parts
local tpart1 = workspace.Touching1 -- RED
local tpart2 = workspace.Touching2 -- BLUE
local tpart3 = workspace.Touching3 -- GREEN

-- Debounce system
local Debounce = false

-- tables
local obj = {}
local bricks = {tpart1, tpart2, tpart3} -- array containing the objects

-- function
local function UpdateFrames(color: Color3)
   local name = "item%d"

   if #obj >= 3 then table.remove(obj, 1) end
   table.insert(obj, color)

   for i, clr in ipairs(obj) do
      local frame = script.Parent:FindFirstChild(name:format(i))

      if frame then
         frame.BackgroundColor3 = clr
      end
   end
end

-- script
for i, brick in ipairs(bricks) do -- loop through them
    local debounce = false

    brick.Touched:Connect(function(hit)
        if not debounce then
            if hit.Parent:FindFirstChildOfClass("Humanoid") then
               debounce = true

               UpdateFrames(brick.Color)
               wait(5)
               debounce = false
            end
        end
    end)
end

This may or may not work, I’m not sure