[Solved] Issues using variables with Color3.fromRGB

I am trying to change the colour of parts by using Color3.fromRGB. I am trying to store two pairs of rgb values within a single variable that stores them within an array, and am trying to parse those into Color3.fromRGB using indexing, however the output complains that it is being given a string for the Color value, even though no string is present?

image

image

Why does this not work? And how can I implement this as I intend?

image
I’ve tried the code shown in the above image as it was a solution to a similar post, yet I get the exact same error, so why on earth is this not working?

1 Like

Firstly, your error tells me that WindowNames.BrightNames is a table of strings. You’re going to need to make sure it’s something with a colour property, such as a Part.

Then, you can just pass your table of colours to Color3.fromRGB like so:

for _, colourTarget in next, WindowNames.BrightNames, nil do --make sure each element in WindowNames.BrightNames has a "Color" property!
    colourTarget.Color = Color3.fromRGB(table.unpack(BrightColour[1]))
end

you can see in the first image its not a table of strings though, so why does it think it is a table of strings?
edit 3: I have realised a potential issue in the code, WindowNames.BrightNames is not the correct array :woman_facepalming:

So, I now have no errors in the output, yet the parts are not having their colours altered, any idea why?
This is the array that contains the parts to be affected:

Right now, you’re setting C.Color to just a table. You should be doing Color3.fromRBG(). So when you C.Color, it errors. However, Color3.fromRBG(), accepts 3 parameters. For example: Color3.fromRBG(170, 150, 85).
So in your case, with your current setup you would have to do Color3.fromRBG(BrightColour[1][1], BrightColour[1][2], BrightColour[1][3]).
An easier solution would be to simply put those table values into Color3.fromRBG(), directly in the table: local BrightColour = {Color3.fromRGB(170, 150, 85)}
That way you can simply access the colors by doing BrightColor[1].
Hope this helps!

So if I used local BrightColour = {Color3.fromRGB(170, 150, 85)} then in the loop I just need to do C.Color = BrightColour? I’ll try that now.

edit: I’ve made those changes, and yet the parts are still not being changed. Could this be because the array BrightWindows storing the parts doesn’t actually address the parts and merely stores their name? If so, how do I rectify this @youwillpay4that @12345koip?
Or is something else the problem?


-- Define the RGB values
local red = 255
local green = 0
local blue = 0

-- Create the Color3 using fromRGB
local color = Color3.fromRGB(red, green, blue)

-- Change the color of the part
local part = workspace:FindFirstChild("ColorPart")
if part then
    part.BrickColor = BrickColor.new(color)
else
    warn("ColorPart not found in workspace")
end


local red = 255
local green = 0
local blue = 0

local color = Color3.fromRGB(red, green, blue)

local part = workspace:FindFirstChild("ColorPart")
if part then
    part.Color = color -- Set the Color3 property directly
else
    warn("ColorPart not found in workspace")
end

Unlike @5Scll1 's suggestion of using FindFirstChild over and over, it would be more memory efficient to create a table of all the parts you want to change the colour of. They are direct memory references (essentially) so it should work. You could tag them all and use CollectionService:GetTagged(), or parent them to a folder and use GetChildren().

As for changing the part colours, like others suggested store a direct Color3.fromRGB colour in your table or simply use table.unpack to convert to Color3 values for the part colours.

local colours: {Color3} = {Color3.fromRGB(r, g, b)}
--or use a two-dimensional table (not array)
local colours: {{number, number, number}} = {{85, 170, 0}}

--then:
for _, part in next, game:GetService("CollectionService"):GetTagged("someTag") do --or GetChildren() depending on your method
    part.Color = colours[1]
    --or
    part.Color = Color3.fromRGB(table.unpack(colours[1]))
end

Which I work that apart when you adding to workspace what I think about maybe

I already have a table of all the parts (BrightWindows) and I’m already storing the colour value as a Color3.fromRGB as was previously suggested…
image
The part colours still do not change and I don’t know why! There’s NO error in the output.

Sorry, I don’t understand what you mean by that…

Storing a table of references to the workspace is better than constantly calling FindFirstChild.

Colour which they have a tab people you can work by yourself. I’m just checking what’s something wrong and I wanna help you. I cannot waste my time but this

Can you try adding sone print statements, etc. to print the parts, and their new colours? The only slight problem i see in that image is that Train might not have any parts in it. Debugging with print statements would help to better locate the issue. Could you also send the corrected script that iterates over the parts and assigns their colour?

There are definitely parts in the model stored in the variable Train.

I’ve already shown the loop section of my script that iterates over the parts and assigns their colours, but I’ll report it again…:
image

Can you send some more of the script, such as the initialisation of Train and WindowNames? All I can think is that the the names may not be correctly assigned, train parts havent loaded (client), etc. I think more of the script would help, especially if you put it in a code block and not as an image.

sorry its 10pm for me its hard for me to concentrate but im trying

this is a server script, so all the parts would be loaded before the script runs.
Train just points to the train model instance.
WindowNames is a dictionary containing two arrays of names, the names are used to build the array of parts to have their colour changed.

What specific parts of the code should I post? I’m pretty sure I’ve posted all the relevant parts of the code.

If Train points directly to an instance, that’s the issue if you arent using Train:GetChildren(), or Train:GetDescendants(). Make sure you are doing this, but it’s odd because if you aren’t you’d probably get the error “Attempt to iterate over an instance value”.

Please send the rest of the variable initialisation and updating code if that doesn’t work, it’s an issue with your variables or constants and would be harder to fix without the full variable reference code.

I’ve just found the issue and gotten the script to work. I was not referencing a dictionary key and only the dictionary (WindowNames) itself, and so it wasn’t actually obtaining anything…

Thanks for your help, and sorry if I’ve kept you longer than necessary because of my stupid mistake! You still helped fix one of the issues with the script, I appreciate it!

You should get some rest now!

1 Like

Glad I could help. Some of the most common issues come from variables and table indexing, which is why i wanted to see the script. Make sure to mark your post as solution so the topic can close.

1 Like