Tab your code correctly
The spaces to the left of code are not random.
A block of code grouped under an if
/function
/for
, etc. all share the same left margin. You increase the indent once per group.
You do this so that you can see, e.g., what happens in this if
?
What code is part of this function? What isn't?
Right now, this is the way your code is grouped.
Notice how it nearly goes off the page, it's so deeply grouped?
That's a problem.
What you want to do
You currently have several nested for
loops, all modifying X
in a non-local way. This is not only messy, it's very, very wrong.
Just the children are not enough, so you do children and children's children and children's children's children. In general, these are called descendants, and you would be better served by just getting a list of all of the descendants.
You can define this recursively quite easily:
1 | function descendants(model, r) |
3 | for _, child in pairs (model:GetChildren()) do |
Thus, you would use, e.g.,
Locals and fors
You should use local variables always.
Whenever you first refer to a variable in a function, you should have a local
in front. This prevents a lot of headaches, and it makes Script Analysis be able to better help you. It also reminds you just how much garbage you're introducing into your script (since definitions are more obvious).
Thus you should use
1 | local x = descendants(m) |
Notice how you use i
over and over, but the only thing you do with it is [i]
? Lua provides iterators to make this much nicer.
1 | for _, obj in pairs ( descendants(mod) ) do |
Use functions
Good code reads like well written prose. - Grady Booch
You have a really long block of code that is just to put textures onto a X[i]
. Instead of having that really long block of code in the middle of your already complicated logic, put it into a function:
01 | function putTextures(part) |
02 | local A = Instance.new( "Texture" ) |
09 | local B = Instance.new( "Texture" ) |
16 | local C = Instance.new( "Texture" ) |
23 | local D = Instance.new( "Texture" ) |
30 | local E = Instance.new( "Texture" ) |
37 | local F = Instance.new( "Texture" ) |
49 | function putTextures(part) |
50 | for _, texture in pairs (script.Textures:GetChildren()) do |
51 | texture:Clone().Parent = part |
Now you can just use putTextures(obj)
and pull the complicated work of putting textures out of the rest of your complicated code.
You can shorten it by eliminating the .Parent
lines; the second parameter to Instance.new
is the parent to use:
1 | local A = Instance.new( "Texture" , part) |
Your answer
It's time for your to go back, think about why it's been so difficult to move forward, and resolve to try again, but this time, keep your stuff clean.
If you're having trouble keeping things manageable, it means you've got to learn about solving the problems you're having.
This answer has suggestions for bringing your code to a much, much better state -- but if I do it for you, I'll spend an hour doing it and you won't learn how two apply these tips yourself.