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

How do i prevent myself from continually defining several variables that are the exact same code?

Asked by
nich17 95
9 years ago

an example of my problem is i keep ending up with my scripts starting like this

local model = game.Workspace.model
local partA = model.partA
local partB = model.partB
local partC = model.partC

and i keep having to repeat that for several lines of code.

0
You could condense it to one script, use global variables (not recommended), or use a loop to do what you want to every member of the model, as shown by Timser. GoldenPhysics 474 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

To do this you would use the :GetChildren() method which returns all the Children of that place as a table, and then use in pairs() to have use this table, as so:

Variable = game.Workspace:GetChildren()

for Table,Index in pairs(Variable) do
    if Index:IsA("Part") then
        Index.Transparency = 0
    end
end
0
The names for your variables are really bad. It should be `for index, key in pairs(tab) do`. Though in practice, it should probably be `for _, part in pairs(children) do`. BlueTaslem 18071 — 9y
0
I thought that the variable before the comma represented the Table, and the variable after the comma represented that part of the Table until it reaches the end, the Index. But hey, anyone can be wrong! Shrekerly 70 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
for i,v in pairs(game.Workspace.model:GetChildren()) do
v.Transparency = 1 -- example of code
end

Answer this question