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

How to simplify script with .Parent and transparency?

Hi, im trying to make a script as simple as possible but i don't have much knowledge in simplifying scripts here's the script i want to simplify:

			script.Parent.Metal1.Transparency = 0
			script.Parent.Metal2.Transparency = 0
			script.Parent.Metal3.Transparency = 0
			script.Parent.Metal4.Transparency = 0
			script.Parent.Metal5.Transparency = 0
			script.Parent.Metal6.Transparency = 0
			script.Parent.Metal7.Transparency = 0

1 comment

0

for i,v in next, script.Parent:GetChildren() do if v.Name:gmatch("Metal") then v.Transparency = 0; end end

2 answers

✓ Accepted answer

Exactly for that pairs loops are used.

Here is how it works in your case:

local table = script.Parent:GetChildren()

for index, metal in pairs(table) do
	print(index, metal)

	if metal:IsA("BasePart") then
		metal.Transparency = 0
	end
end

--Output
-- not sure but should be like this:

1 Instance
2 Instance
3 Instance
4 Instance

So in this case, index is variable for the spot that variable has in table and metal is the instance in the table. It checks if metal is a part by doing metal:IsA("BasePart") because if there are for example scripts in the table like in your case, it will error. :GetChildren() gets the children of the instance and returns table with them, instead of that you can also use:

local table = {
	instance1,
	instance2,
}

And like you need.

You can read about loops here: super link

2 comments

0

thats all good but one of the metals are a union

0

So instead you can put them into a folder, having scripts in same place as parts is not exactly the most perfect thing. OR you can do :IsA("UnionOperation") 

You could use a variable.

local p = script.Parent
p.Metal1.Transparency = 0
p.Metal2.Transparency = 0
p.Metal3.Transparency = 0
p.Metal4.Transparency = 0
p.Metal5.Transparency = 0
p.Metal6.Transparency = 0
p.Metal7.Transparency = 0

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