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

Add Texture to nearby parts?

Asked by 8 years ago

I've recently been trying to add texture to all sides of nearby parts, I also have been trying to add texture to parts that may be inside nearby models. For some reason the textures are only added directly to the model itself when ran. Is there any way I can get my script to work?

001me = game.Players.LocalPlayer.Character
002bin = script.Parent
003 
004function onButton1Down(mouse)
005if not enabled then
006                return
007        end
008 
009 
010   local player = game.Players.LocalPlayer
011        if player == nil then return end
012       enabled = false
013 
014 
015 
View all 267 lines...

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 8 years ago

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:

1function descendants(model, r)
2    r= r or {}
3    for _, child in pairs(model:GetChildren()) do
4        table.insert(r, child)
5        descendants(child, r)
6    end
7    return r
8end

Thus, you would use, e.g.,

1X = descendants(m)
2 
3-- instead of
4X = m:GetChildren()

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)
2    for i = 1, #x do
3        blah( x[i] )
4    end
5 
6-- NOT
7    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.

1for _, obj in pairs( descendants(mod) ) do
2    dostuffwith( obj )
3end
4 
5-- INSTEAD OF
6x = descendants(mod)
7for i = 1, #x do
8    dostuffwith( x[i] )
9end

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:

01function putTextures(part)
02    local A = Instance.new("Texture")
04    A.StudsPerTileV = 2
05    A.StudsPerTileU = 2
06    A.Face = "Top"
07    A.Transparency = 0.2
08    A.Parent = part
09    local B = Instance.new("Texture")
11    B.StudsPerTileV = 2
12    B.StudsPerTileU = 2
13    B.Face = "Bottom"
14    B.Transparency = 0.2
15    B.Parent = X[i]
View all 53 lines...

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:

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

Ad

Answer this question