How would I get the first color to run in the table? [SEE EDIT AT BOTTOM AND NEW REPLY]

I was trying to make a pixel art game as a side project. I was trying to make a script that automatically generates the colors so I can edit them in one place, instead of going into every button and changing them. Here’s an image of my UI right now (I will add more buttons when I know this works) The different buttons are supposed to be the different color options.

And then here's my local script
local colors = {
	"1,1,1",
	"0,0,0"
}

local frame = script.Parent
local children = frame:GetChildren()

for x = 1, #children ,1 do
	local isButton = true
	if children[x].Name ~= "TextButton" then
		isButton=false
	end
	if isButton then
		children[x].BackgroundColor3 = Color3.new(colors[x])
	end
	
end

Lastly, here’s a video of me starting the game:

EDIT: Okay I see the problem. There is more than one child of that frame. That is causing the colors table[x] to be 2 or more, causing it to be the second color. How would I fix this?

NEWEST EDIT LOL:
I have moved the color list number into a separate variable. It is now coming out red, which is still unexpected.

local colors = {
	1,1,1,
	255,0,0
}

local frame = script.Parent
local children = frame:GetChildren()
local colorListNumber = 1

for x = 1, #children ,1 do
	local isButton = true
	if children[x].Name ~= "TextButton" then
		isButton=false
	end
	if isButton then
		children[x].BackgroundColor3 = Color3.new(colors[colorListNumber])
		colorListNumber=colorListNumber+1
	end
end

I don’t know if this will fix your issue but you might as well try, try changing your current script to this one:

 local colors = {
        	"1,1,1",
        	"255,0,0"
        }

        local frame = script.Parent
        local children = frame:GetChildren()
        local colorListNumber = 1

        for x = 1, #children ,1 do
        	local isButton = true
        	if children[x].Name ~= "TextButton" then
        		isButton=false
        	end
        	if isButton then
        		children[x].BackgroundColor3 = Color3.new(tonumber(colors[colorListNumber]))
        		colorListNumber=colorListNumber+1
        	end
        end

Your issue is probably the usage of commas in the table

You also need to change your Color3.new value to Color3.FromRGB

Alright they turned back to black and that isn’t even in the table anymore???

local colors = {
	1,1,1,
	255,0,0
}

local frame = script.Parent
local children = frame:GetChildren()
local colorListNumber = 1

for x = 1, #children ,1 do
	local isButton = true
	if children[x].Name ~= "TextButton" then
		isButton=false
	end
	if isButton then
		children[x].BackgroundColor3 = Color3.fromRGB(colors[tonumber(colorListNumber)])
		colorListNumber=colorListNumber+1
	end
end

Use strings

"1,1,1",
"255,0,0"

The function of tonumber in case you didn’t know is to change the strings to integer values

Could you not just do this?

local Colors = {
Color3.fromRGB(255, 255, 255),
Color3.fromRGB(255, 0, 0)
}

local Frame = script.Parent.Frame
local ColorListNumber = 1
for _, Button in pairs(Frame:GetChildren()) do
if Button:IsA("TextButton") then
Button.BackgroundColor3 = Colors[ColorListNumber]
ColorListNumber += 1
end
end

Use just RGB colors, you have one Color3 value and one RGB value

I was just taking a look at an RGB calculator and Color3.fromRGB returns the color black. Perhaps you can test for the other color in the table aka the red color by changing this value to 2

I see what you mean. 1,1,1 in RGB would turn out to be black. I changed Color3.FromRGB to Color3.FromHSV

So why is this still returning as only white when the list number is 1 or 2 then?

local colors = {
	"1,1,1",
	"255,0,0"
}

local frame = script.Parent
local children = frame:GetChildren()
local colorListNumber = 1

for x = 1, #children ,1 do
	local isButton = true
	if children[x].Name ~= "TextButton" then
		isButton=false
	end
	if isButton then
		children[x].BackgroundColor3 = Color3.fromHSV(colors[tonumber(colorListNumber)])
		colorListNumber=colorListNumber+1
	end
end

Firstly, I’d recommend using RGB over HSV as that’s more popular. Secondly I’d recommend changing your script to what @bluddbrain suggested since that’s less complex than yours.

1 Like

Alright well this one isn’t working either. I don’t see why it isn’t tho

Can you send your current code?

It’s the same as what bubbley said

Did you update this?
And do you get any errors?

Alright it’s fine now the problem was the frame variable wasn’t directing to the right place. Thanks. I will learn from this new code.

Make sure to mark problem as resolved to help other people who come by

Main Answer:

Other main helper: @SaifLachmann

You have to click the “solution” icon in order to put it as a solution and give the user a small reward in form of you giving him the Solution mark.