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

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

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.

1 comment

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.

2 answers

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

2 comments

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.

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!

for i,v in pairs(game.Workspace.model:GetChildren()) do
v.Transparency = 1 -- example of code
end

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