Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a faster and more efficient way of making this function?

Asked by 6 years ago
Edited 6 years ago

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-------
02local longhairs = hats["Long Hair"]
03local longhair = longhairs["Long Hair"].Handle.Mesh
04--Color
05local longhair1 = longhairs["1"].ColorPicker
06local longhair2 = longhairs["2"].ColorPicker
07local longhair3 = longhairs["3"].ColorPicker
08local longhair4 = longhairs["4"].ColorPicker
09local longhair5 = longhairs["5"].ColorPicker
10local longhair6 = longhairs["6"].ColorPicker
11local longhair7 = longhairs["7"].ColorPicker
12local longhair8 = longhairs["8"].ColorPicker
13--1
14longhair1.MouseClick:Connect(function(player)
15    longhair.VertexColor = Vector3.new(1, 1, 1)
View all 44 lines...

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?

0
try using a numeric for loop theking48989987 2147 — 6y
0
How would I do that? BlauKitten 88 — 6y
0
some thing like for i=1, #longhairs:GetChildren() do end theking48989987 2147 — 6y
0
I'll check that out. BlauKitten 88 — 6y

2 answers

Log in to vote
2
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago
1--Your variables
2local longhairs = hats["Long Hair"]
3local 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#]
02local vectors = {
03    Vector3.new(1, 1, 1),
04    Vector3.new(0.7, 0, 1),
05    Vector3.new(0.68, 0.484, 0.275),
06    Vector3.new(0.4, 0.292, 0.236),
07    Vector3.new(0.185, 0.183, 0.184),
08    Vector3.new(1, 0.941, 0.705),
09    Vector3.new(0.375, 0.128, 0.112),
10    Vector3.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.
2for i=1,8 do
3    local curLongHair = longhairs[tostring(i)]
4    curLongHair.MouseClick:Connect(function(player)
5        longhair.VertexColor = vectors[i]
6    end)
7end

Complete Code

01--Your variables
02local longhairs = hats["Long Hair"]
03local longhair = longhairs["Long Hair"].Handle.Mesh
04 
05--Table with the vectors. Their position in the table represents longhairs[position#]
06local vectors = {
07    Vector3.new(1, 1, 1),
08    Vector3.new(0.7, 0, 1),
09    Vector3.new(0.68, 0.484, 0.275),
10    Vector3.new(0.4, 0.292, 0.236),
11    Vector3.new(0.185, 0.183, 0.184),
12    Vector3.new(1, 0.941, 0.705),
13    Vector3.new(0.375, 0.128, 0.112),
14    Vector3.new(1, 0.7, 1)
15}
View all 23 lines...

Please ask questions if you have any.

0
Explain your code a bit so he can learn Shawnyg 4330 — 6y
0
@Shawnyg edited. DanzLua 2879 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

First, create a table with all the vertex colours included for each long hair thing:

1local 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

01for 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)
13end

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

Answer this question