How to change trail's color based on Item is "MainColor" Value in Module Script

Okay so I want to change Trail’s color depends on “MainColor” value


(Don’t Mind ID and Name)

like

if mainColor = "Blue" then 
 color1 = Color3.fromRGB(15, 127, 254) 
 color2 = Color3.fromRGB(255, 255, 255)    
trail.Color = ColorSequence.new(color1, color2) 

I used something like that but Idk if it is okay to use this because There is like 8-9 of them and I don’t want to use that much code space and make server script busy is there any easier way to do that?

for _,i in ipairs(toolConfig) do -- ToolConfig is the Module that has MainColor variable
			if i.MainColor == "Blue" then
			color1 = Color3.fromRGB(15, 127, 254)
			color2 = Color3.fromRGB(255, 255, 255)
		end
	end

here is some details if you need
script is parented to a tool
attachments and trail are child of meshpart which is a child of that tool
Screenshot_105

I tried using this but even the tool I am equipped’s MainColor value is “Purple” trail shows the “Red” s color

  for _,i in ipairs(toolConfig) do
				if i.MainColor == "Purple" then
                     color1 = i.Color1
					color2 = i.Color2
				elseif i.MainColor == "Red" then
					color1 = i.Color1
					color2 = i.Color2
				end
			end
			trail.Color = ColorSequence.new(color1, color2)

Don’t have an idea why

The table with purple does not have a main color set.

hey is the problem fix now?

I added the Purple later. and Nope.

				if i.MainColor == "Purple" then
                     color1 = i.Color1
					color2 = i.Color2
				elseif i.MainColor == "Red" then
					color1 = i.Color1
					color2 = i.Color2
				end
print(i.MainColor)

see what it prints out
maybe the maincolor just doesn’t exist or doesn’t equal purple for some reason

well the weird part is If I Use this trail Shows yellow’s colors

if i.MainColor == "Purple" then
                     color1 = i.Color1
					color2 = i.Color2
				elseif i.MainColor == "Red" then
					color1 = i.Color1
					color2 = i.Color2
				elseif i.MainColor == "Yellow" then
                                        color1 = i.Color1
                                        color2 = i.Color2
end

btw Print(i.MainColor)
printed this

I got the issue There is Yellow Value and because of elseif it doesn’t care about previous if statements and shows the yellow. But like I said I don’t know how to fix it

add a print(i.Color1, i.Color2) i have a feeling that it will be the same when using purple or red tool

it will be same color no matter which tool I use. But I want to change it depends on tool I am using. and yeah the problem is how to fix it

try to remove the elseif stuff
just make all colors have a separate if statement

if i.MainColor == "Purple" then
        color1 = i.Color1
	color2 = i.Color2
end
if i.MainColor == "Red" then
	color1 = i.Color1
	color2 = i.Color2
end
if i.MainColor == "Yellow" then
        color1 = i.Color1
        color2 = i.Color2
end

I think Problem is caused because of ipairs not with if Statements or elseif’s

for _,i in ipairs(toolConfig) do
				if i.MainColor == "Purple" then
                     color1 = i.Color1
		color2 = i.Color2
				end
			end

	
	for _,i in ipairs(toolConfig) do
		if i.MainColor == "Red" then
			color1 = i.Color1
			color2 = i.Color2
		end
	end
		trail.Color = ColorSequence.new(color1, color2)

Trail is still red color btw

for _,i in pairs(toolConfig) do
	if i.MainColor == "Purple" then
		color1 = i.Color1
		color2 = i.Color2
	end
	
	if i.MainColor == "Red" then
		color1 = i.Color1
		color2 = i.Color2
	end
end

trail.Color = ColorSequence.new(color1, color2)

combine the if statement into 1 loop and change it too just pairs like this (tell me if it works)

it didn’t work.
Still red color

for _,i in pairs(toolConfig) do
	if i.MainColor == "Purple" then
		color1 = i.Color1
		color2 = i.Color2
		
		print(i.Color1)
		print(i.Color2)
	end
	
	if i.MainColor == "Red" then
		color1 = i.Color1
		color2 = i.Color2
		
		print(i.Color1)
		print(i.Color2)
	end
end

trail.Color = ColorSequence.new(color1, color2)

print out the i.Color1 and 2, check if the colors are purple

color’s are printed different but you are not understanding the problem

Since there is a “Red” value in Tool Module No matter what happens it will show the red is color
If I change the red with SomethingLike “KomodaDragon” it will Show purple color because there is no “KomodaDragon” in the module and If I change places of Red and Purple it will show purple trail because of order.

I want to take MainColor of my equipped Tool
and if That tool’s value equals to purple, it should show purple color

But the thing ipairs do is checks the whole module and looks for the MainColor value inside that module

you need to add a string value in each tool that has the color name.

for example, the tool that needs to be purple will have a string value named “toolColor” and the value of that will be “Purple”

local toolValue = tool.toolColor.Value

for _,i in ipairs(toolConfig) do
	if i.MainColor == toolValue then
		color1 = i.Color1
		color2 = i.Color2
	end
end

trail.Color = ColorSequence.new(color1, color2)

here’s a picture of what i mean

image

There should be a easier way Because tracking every tool’s toolColor.Value by scripts is not a good way like by this way you have to Change every toolColor.Value manually and if there is 100 tools, welp not a good way.

you need something in the tool that has the value of the color
it could be a value like I said, or something in the name like “blueScythe”

is there any part of the tool that can tell you the color the trail should be?
(something in the name or a value in the tool)

otherwise that’s what your going to have to do