I've designed a script to change the color of hair that I've set up in a morph room. It works fine in all but, the script for the color swap is getting packed(Almost 1000 lines) and theres a delay. I feel that there has to be a more efficient way to doing this.
This is a part of the script for one of the hairs.
-------Long Hair------- local longhairs = hats["Long Hair"] local longhair = longhairs["Long Hair"].Handle.Mesh --Color local longhair1 = longhairs["1"].ColorPicker local longhair2 = longhairs["2"].ColorPicker local longhair3 = longhairs["3"].ColorPicker local longhair4 = longhairs["4"].ColorPicker local longhair5 = longhairs["5"].ColorPicker local longhair6 = longhairs["6"].ColorPicker local longhair7 = longhairs["7"].ColorPicker local longhair8 = longhairs["8"].ColorPicker --1 longhair1.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(1, 1, 1) end) --2 longhair2.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(0.7, 0, 1) end) --3 longhair3.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(0.68, 0.484, 0.275) end) --4 longhair4.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(0.4, 0.292, 0.236) end) --5 longhair5.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(0.185, 0.183, 0.184) end) --6 longhair6.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(1, 0.941, 0.705) end) --7 longhair7.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(0.375, 0.128, 0.112) end) --8 longhair8.MouseClick:Connect(function(player) longhair.VertexColor = Vector3.new(1, 0.7, 1) end)
Theres a bunch of buttons that these are connected to that swap out the color. Is there a way to shorten this code to check what button is being pressed and for what color to be used?
--Your variables local longhairs = hats["Long Hair"] local longhair = longhairs["Long Hair"].Handle.Mesh
Here we have a table with the vectors used for the VectorColor in the mesh. Their position in the table represents longhairs[position#] which we can get to with the For loop, created later on down.
--Table with the vectors. Their position in the table represents longhairs[position#] local vectors = { Vector3.new(1, 1, 1), Vector3.new(0.7, 0, 1), Vector3.new(0.68, 0.484, 0.275), Vector3.new(0.4, 0.292, 0.236), Vector3.new(0.185, 0.183, 0.184), Vector3.new(1, 0.941, 0.705), Vector3.new(0.375, 0.128, 0.112), Vector3.new(1, 0.7, 1) }
Now we create the for loop, since you have 8 buttons we will create the loop to run 8 times.
the variable "curLongHair' points to the button by using the current iteration 'i' as what button we want.
We then create the mouseclick event for that button and change its VertexColor onclick to the Vector found in the table above using 'i'.
--For loop that will create the mouseclick event for each of the buttons. for i=1,8 do local curLongHair = longhairs[tostring(i)] curLongHair.MouseClick:Connect(function(player) longhair.VertexColor = vectors[i] end) end
Complete Code
--Your variables local longhairs = hats["Long Hair"] local longhair = longhairs["Long Hair"].Handle.Mesh --Table with the vectors. Their position in the table represents longhairs[position#] local vectors = { Vector3.new(1, 1, 1), Vector3.new(0.7, 0, 1), Vector3.new(0.68, 0.484, 0.275), Vector3.new(0.4, 0.292, 0.236), Vector3.new(0.185, 0.183, 0.184), Vector3.new(1, 0.941, 0.705), Vector3.new(0.375, 0.128, 0.112), Vector3.new(1, 0.7, 1) } --For loop that will create the mouseclick event for each of the buttons. for i=1,8 do local curLongHair = longhairs[tostring(i)] curLongHair.MouseClick:Connect(function(player) longhair.VertexColor = vectors[i] end) end
Please ask questions if you have any.
First, create a table with all the vertex colours included for each long hair thing:
local longHairColours = {Vector3.new(1, 1, 1), Vector3.new(0.7, 0, 1), etc}
Loop through each member of longhairs with a numeric for loop (as that king guy said) like this (I changed your variable names to look much nicer btw lol)
then just bind the mouse click to the anonymous function which as you normally would inside each iteration
for i = 1, 8 do -- v = value of the item in the table -- i = index or position of the item in the table -- Get long hair, and vertex colour local longHair = longHairs[tostring(i)] local vertexColour = longHairColours[i] -- Bind click function longHair.MouseClick:Connect(function() -- we dont need to use the player argument longHair.VertexColor = vertexColour end) end
you can shorten this script if you want because the variables i defined aren't really necessary since they're only used once.
contact me on discord if you need more help since i literally came here to procrastinate: sub#2251