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.
01 | -------Long Hair------- |
02 | local longhairs = hats [ "Long Hair" ] |
03 | local longhair = longhairs [ "Long Hair" ] .Handle.Mesh |
04 | --Color |
05 | local longhair 1 = longhairs [ "1" ] .ColorPicker |
06 | local longhair 2 = longhairs [ "2" ] .ColorPicker |
07 | local longhair 3 = longhairs [ "3" ] .ColorPicker |
08 | local longhair 4 = longhairs [ "4" ] .ColorPicker |
09 | local longhair 5 = longhairs [ "5" ] .ColorPicker |
10 | local longhair 6 = longhairs [ "6" ] .ColorPicker |
11 | local longhair 7 = longhairs [ "7" ] .ColorPicker |
12 | local longhair 8 = longhairs [ "8" ] .ColorPicker |
13 | --1 |
14 | longhair 1. MouseClick:Connect( function (player) |
15 | longhair.VertexColor = Vector 3. new( 1 , 1 , 1 ) |
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?
1 | --Your variables |
2 | local longhairs = hats [ "Long Hair" ] |
3 | 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.
01 | --Table with the vectors. Their position in the table represents longhairs[position#] |
02 | local vectors = { |
03 | Vector 3. new( 1 , 1 , 1 ), |
04 | Vector 3. new( 0.7 , 0 , 1 ), |
05 | Vector 3. new( 0.68 , 0.484 , 0.275 ), |
06 | Vector 3. new( 0.4 , 0.292 , 0.236 ), |
07 | Vector 3. new( 0.185 , 0.183 , 0.184 ), |
08 | Vector 3. new( 1 , 0.941 , 0.705 ), |
09 | Vector 3. new( 0.375 , 0.128 , 0.112 ), |
10 | Vector 3. new( 1 , 0.7 , 1 ) |
11 | } |
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'.
1 | --For loop that will create the mouseclick event for each of the buttons. |
2 | for i = 1 , 8 do |
3 | local curLongHair = longhairs [ tostring (i) ] |
4 | curLongHair.MouseClick:Connect( function (player) |
5 | longhair.VertexColor = vectors [ i ] |
6 | end ) |
7 | end |
Complete Code
01 | --Your variables |
02 | local longhairs = hats [ "Long Hair" ] |
03 | local longhair = longhairs [ "Long Hair" ] .Handle.Mesh |
04 |
05 | --Table with the vectors. Their position in the table represents longhairs[position#] |
06 | local vectors = { |
07 | Vector 3. new( 1 , 1 , 1 ), |
08 | Vector 3. new( 0.7 , 0 , 1 ), |
09 | Vector 3. new( 0.68 , 0.484 , 0.275 ), |
10 | Vector 3. new( 0.4 , 0.292 , 0.236 ), |
11 | Vector 3. new( 0.185 , 0.183 , 0.184 ), |
12 | Vector 3. new( 1 , 0.941 , 0.705 ), |
13 | Vector 3. new( 0.375 , 0.128 , 0.112 ), |
14 | Vector 3. new( 1 , 0.7 , 1 ) |
15 | } |
Please ask questions if you have any.
First, create a table with all the vertex colours included for each long hair thing:
1 | local longHairColours = { Vector 3. new( 1 , 1 , 1 ), Vector 3. 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
01 | for i = 1 , 8 do |
02 | -- v = value of the item in the table |
03 | -- i = index or position of the item in the table |
04 |
05 | -- Get long hair, and vertex colour |
06 | local longHair = longHairs [ tostring (i) ] |
07 | local vertexColour = longHairColours [ i ] |
08 |
09 | -- Bind click function |
10 | longHair.MouseClick:Connect( function () -- we dont need to use the player argument |
11 | longHair.VertexColor = vertexColour |
12 | end ) |
13 | 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