Trying to change a color3 value by using variables

I’m working on a small project and I thought of adding some color customization. How can I use variables as RGB values, and/or how can i get a variable from a textbox input?

Here’s the code:


local x = bckg.Frame.x.TextBox.Text
local y = bckg.Frame.y.TextBox.Text
local z = bckg.Frame.z.TextBox.Text

bckg.BackgroundColor3 = Color3.fromRGB(x, y, z)
1 Like

You either need to set the color when the text of x, y, or z changes with TextBox:GetPropertyChangedSignal'Text' or make a button that you click that updates the colors

1 Like

You can’t do that. When referencing the TextBox.Text part, you basically make a variable that contains whatever TextBox.Text was at that time. If the text changes, it won’t work.

Try this:

local x = bckg.Frame.x.TextBox
local y = bckg.Frame.y.TextBox
local z = bckg.Frame.z.TextBox

bckg.BackgroundColor3 = Color3.fromRGB(tonumber(x.Text), tonumber(y.Text), tonumber(z.Text))

Don’t forget to use tonumber(), the textbox text is a string, and Color3.fromRGB(x,y,z) takes in int as arguments.

1 Like

Try doing like this

local x = bckg.Frame.x.TextBox.TextColor3.R
local y = bckg.Frame.y.TextBox.TextColor3.G
local z = bckg.Frame.z.TextBox.TextColor3.B

bckg.BackgroundColor3 = Color3.fromRGB(x, y, z)

So basically, there is 2 “APIs” on ROBLOX.

Let me introduce them to you:

tostring() and tonumber()

Now, what you did is get the string of the TextBox and you didn’t convert it into a number. To convert that variable, you can just do “local x,y,z = tonumber(bckg.Frame.x.TextBox.Text), tonumber(bckg.Frame.y.TextBox.Text), tonumber(bckg.Frame.z.TextBox.Text)”.

I hope that helped, just remember that the parameters of Color3 are numbers and not strings!!!

A little correction. tostring and tonumber are Luau global functions, not APIs.

1 Like
local colors = {
x = bckg.Frame.x,
y = bckg.Frame.y,
z = bckg.Frame.z
}

function changeColor()
local x, y, z = colors[1].Text, colors[2].Text, colors[3].Text

bckg.BackgroundColor3 = Color3.fromRGB(
tonumber(x) or 255,
tonumber(y) or 255,
tonumber(z) or 255
)
end

for _, textBox in pairs(colors) do
textBox:GetPropertyChangedSignal('Text'):Connect(changeColor)
end

I know what they are, I made a mistake and forgot that existed. Let me edit my post real quick.