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.
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
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
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
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
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.