Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Add Texture to nearby parts?

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?

 me = game.Players.LocalPlayer.Character
bin = script.Parent

function onButton1Down(mouse)
if not enabled then
                return
        end

      
   local player = game.Players.LocalPlayer
        if player == nil then return end
       enabled = false
    


for u, c in pairs(workspace:GetChildren()) do
      h = workspace:GetChildren()
      t = c:findFirstChild("Torso")
if h ~= nil and t ~= nil then
       
for i = 1, #h do
	if h[i]:IsA("Model") then 
		X = h[i]:GetChildren()
	               for i = 1, #X do
		if X[i]:IsA("Model") then 
		Z = X[i]:GetChildren()
		
		for i = 1, #X do
			
		if X[i]:IsA("Part") or X[i]:IsA("UnionOperation") then
	    if (X[i].Position-t.Position).magnitude <= 60 then
		if (X[i].Position-t.Position).magnitude >= 60 then return end
		if X[i]:IsA("Part") or X[i]:IsA("UnionOperation") then
			print("bruh")
		for i = 1, #X do
        		A = Instance.new("Texture")
				A.Texture = "http://www.roblox.com/asset/?id=108208815"
				A.StudsPerTileV = 2
				A.StudsPerTileU = 2
				A.Face = "Top"
				A.Transparency = 0.2
				A.Parent = X[i]
				B = Instance.new("Texture")
				B.Texture = "http://www.roblox.com/asset/?id=108208815"
				B.StudsPerTileV = 2
				B.StudsPerTileU = 2
				B.Face = "Bottom"
				B.Transparency = 0.2
				B.Parent = X[i]
				C = Instance.new("Texture")
				C.Texture = "http://www.roblox.com/asset/?id=108208815"
				C.StudsPerTileV = 2
				C.StudsPerTileU = 2
				C.Face = "Front"
				C.Transparency = 0.2
				C.Parent = X[i]
				D = Instance.new("Texture")
				D.Texture = "http://www.roblox.com/asset/?id=108208815"
				D.StudsPerTileV = 2
				D.StudsPerTileU = 2
				D.Face = "Back"
				D.Transparency = 0.2
				D.Parent = X[i]
				E = Instance.new("Texture")
				E.Texture = "http://www.roblox.com/asset/?id=108208815"
				E.StudsPerTileV = 2
				E.StudsPerTileU = 2
				E.Face = "Left"
				E.Transparency = 0.2
				E.Parent = X[i]
				F = Instance.new("Texture")
				F.Texture = "http://www.roblox.com/asset/?id=108208815"
				F.StudsPerTileV = 2
				F.StudsPerTileU = 2
				F.Face = "Right"
				F.Transparency = 0.2
				F.Parent = X[i]
				wait(.15)
				end
	    for i = 1, #Z do  
		if Z[i]:IsA("BasePart") or Z[i]:IsA("UnionOperation") then
			print("test")
	    if (Z[i].Position-t.Position).magnitude <= 60 then
		if (Z[i].Position-t.Position).magnitude >= 60 then return end 
			if Z[i]:IsA("BasePart") or Z[i]:IsA("UnionOperation") then
			for i = 1, #Z do
			    G = Instance.new("Texture")
				G.Texture = "http://www.roblox.com/asset/?id=108208815"
				G.StudsPerTileV = 2
				G.StudsPerTileU = 2
				G.Face = "Top"
				G.Transparency = 0.2
				G.Parent = Z[i]
				H = Instance.new("Texture")
				H.Texture = "http://www.roblox.com/asset/?id=108208815"
				H.StudsPerTileV = 2
				H.StudsPerTileU = 2
				H.Face = "Bottom"
				H.Transparency = 0.2
				H.Parent = Z[i]
				L = Instance.new("Texture")
				L.Texture = "http://www.roblox.com/asset/?id=108208815"
				L.StudsPerTileV = 2
				L.StudsPerTileU = 2
				L.Face = "Front"
				L.Transparency = 0.2
				L.Parent = Z[i]
				M = Instance.new("Texture")
				M.Texture = "http://www.roblox.com/asset/?id=108208815"
				M.StudsPerTileV = 2
				M.StudsPerTileU = 2
				M.Face = "Back"
				M.Transparency = 0.2
				M.Parent = Z[i]
				N = Instance.new("Texture")
				N.Texture = "http://www.roblox.com/asset/?id=108208815"
				N.StudsPerTileV = 2
				N.StudsPerTileU = 2
				N.Face = "Left"
				N.Transparency = 0.2
				N.Parent = Z[i]
				O = Instance.new("Texture")
				O.Texture = "http://www.roblox.com/asset/?id=108208815"
				O.StudsPerTileV = 2
				O.StudsPerTileU = 2
				O.Face = "Right"
				O.Transparency = 0.2
		        O.Parent = Z[i]
				wait(.15)
				end
				print("yo")
				if Z[i]:IsA("Model") then 
		K = Z[i]:GetChildren()
			for i = 1, #K do  
		if K[i]:IsA("BasePart") or K[i]:IsA("UnionOperation") then
	    if (K[i].Position-t.Position).magnitude <= 60 then
		if (K[i].Position-t.Position).magnitude >= 60 then return end
			if K[i]:IsA("BasePart") or K[i]:IsA("UnionOperation") then
			for i = 1, #K do
			AA = Instance.new("Texture")
				AA.Texture = "http://www.roblox.com/asset/?id=108208815"
				AA.StudsPerTileV = 2
				AA.StudsPerTileU = 2
				AA.Face = "Top"
				AA.Transparency = 0.2
				AA.Parent = K[i]
				BB = Instance.new("Texture")
				BB.Texture = "http://www.roblox.com/asset/?id=108208815"
				BB.StudsPerTileV = 2
				BB.StudsPerTileU = 2
				BB.Face = "Bottom"
				BB.Transparency = 0.2
				BB.Parent = K[i]
				CC = Instance.new("Texture")
				CC.Texture = "http://www.roblox.com/asset/?id=108208815"
				CC.StudsPerTileV = 2
				CC.StudsPerTileU = 2
				CC.Face = "Front"
				CC.Transparency = 0.2
				CC.Parent = K[i]
				DD = Instance.new("Texture")
				DD.Texture = "http://www.roblox.com/asset/?id=108208815"
				DD.StudsPerTileV = 2
				DD.StudsPerTileU = 2
				DD.Face = "Back"
				DD.Transparency = 0.2
				DD.Parent = K[i]
				EE = Instance.new("Texture")
				EE.Texture = "http://www.roblox.com/asset/?id=108208815"
				EE.StudsPerTileV = 2
				EE.StudsPerTileU = 2
				EE.Face = "Left"
				EE.Transparency = 0.2
				EE.Parent = K[i]
				FF = Instance.new("Texture")
				FF.Texture = "http://www.roblox.com/asset/?id=108208815"
				FF.StudsPerTileV = 2
				FF.StudsPerTileU = 2
				FF.Face = "Right"
				FF.Transparency = 0.2
				FF.Parent = K[i]
				print("hi")
				wait(.15)
				end
	        for i = 1, #X do
		if X[i]:IsA("Part") or X[i]:IsA("UnionOperation") then 
			 if (X[i].Position-t.Position).magnitude <= 60 then
				print("not I")
				if (X[i].Position-t.Position).magnitude <= 60 then return end
		if X[i]:IsA("Part") or X[i]:IsA("UnionOperation") then
		for i = 1, #X do
        		A = Instance.new("Texture")
				A.Texture = "http://www.roblox.com/asset/?id=108208815"
				A.StudsPerTileV = 2
				A.StudsPerTileU = 2
				A.Face = "Top"
				A.Transparency = 0.2
				A.Parent = X[i]
				B = Instance.new("Texture")
				B.Texture = "http://www.roblox.com/asset/?id=108208815"
				B.StudsPerTileV = 2
				B.StudsPerTileU = 2
				B.Face = "Bottom"
				B.Transparency = 0.2
				B.Parent = X[i]
				C = Instance.new("Texture")
				C.Texture = "http://www.roblox.com/asset/?id=108208815"
				C.StudsPerTileV = 2
				C.StudsPerTileU = 2
				C.Face = "Front"
				C.Transparency = 0.2
				C.Parent = X[i]
				D = Instance.new("Texture")
				D.Texture = "http://www.roblox.com/asset/?id=108208815"
				D.StudsPerTileV = 2
				D.StudsPerTileU = 2
				D.Face = "Back"
				D.Transparency = 0.2
				D.Parent = X[i]
				E = Instance.new("Texture")
				E.Texture = "http://www.roblox.com/asset/?id=108208815"
				E.StudsPerTileV = 2
				E.StudsPerTileU = 2
				E.Face = "Left"
				E.Transparency = 0.2
				E.Parent = X[i]
				F = Instance.new("Texture")
				F.Texture = "http://www.roblox.com/asset/?id=108208815"
				F.StudsPerTileV = 2
				F.StudsPerTileU = 2
				F.Face = "Right"
				F.Transparency = 0.2
				F.Parent = X[i]
				wait(.15)
		end
		end
		end
	end
	end
	end
		end
		end
		end
	end
end
end
		end 
		end
		end
		end
		end
		end
		end
		end
	end 
	end
end
end 
 enabled = true

        end

enabled = true 
function onS(mouse) 
mouse.Button1Down:connect(function() onButton1Down(mouse) end) 
end 
bin.Selected:connect(onS)


1 answer

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:

function descendants(model, r)
	r= r or {}
	for _, child in pairs(model:GetChildren()) do
		table.insert(r, child)
		descendants(child, r)
	end
	return r
end

Thus, you would use, e.g.,

X = descendants(m)

-- instead of
X = 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

	local x = descendants(m)
	for i = 1, #x do
		blah( x[i] )
	end

-- NOT
	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.

for _, obj in pairs( descendants(mod) ) do
	dostuffwith( obj )
end

-- INSTEAD OF
x = descendants(mod)
for i = 1, #x do
	dostuffwith( x[i] )
end

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:

function putTextures(part)
	local A = Instance.new("Texture")
	A.Texture = "http://www.roblox.com/asset/?id=108208815"
	A.StudsPerTileV = 2
	A.StudsPerTileU = 2
	A.Face = "Top"
	A.Transparency = 0.2
	A.Parent = part
	local B = Instance.new("Texture")
	B.Texture = "http://www.roblox.com/asset/?id=108208815"
	B.StudsPerTileV = 2
	B.StudsPerTileU = 2
	B.Face = "Bottom"
	B.Transparency = 0.2
	B.Parent = X[i]
	local C = Instance.new("Texture")
	C.Texture = "http://www.roblox.com/asset/?id=108208815"
	C.StudsPerTileV = 2
	C.StudsPerTileU = 2
	C.Face = "Front"
	C.Transparency = 0.2
	C.Parent = part
	local D = Instance.new("Texture")
	D.Texture = "http://www.roblox.com/asset/?id=108208815"
	D.StudsPerTileV = 2
	D.StudsPerTileU = 2
	D.Face = "Back"
	D.Transparency = 0.2
	D.Parent = part
	local E = Instance.new("Texture")
	E.Texture = "http://www.roblox.com/asset/?id=108208815"
	E.StudsPerTileV = 2
	E.StudsPerTileU = 2
	E.Face = "Left"
	E.Transparency = 0.2
	E.Parent = part
	local F = Instance.new("Texture")
	F.Texture = "http://www.roblox.com/asset/?id=108208815"
	F.StudsPerTileV = 2
	F.StudsPerTileU = 2
	F.Face = "Right"
	F.Transparency = 0.2
	F.Parent = part
end

-- OR BETTER YET!
-- just keep them as objects inside the script!

function putTextures(part)
	for _, texture in pairs(script.Textures:GetChildren()) do
		texture:Clone().Parent = part
	end
end

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:

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.