How do you change a group of parts that are under a group with a script? I have parts that I have grouped together and they are under a model / group. I want to change their transparency all at once in a script but I don't know how to call them all at once. Each part has the same name.
You could use a for loop
that runs through the model children and change their transparency one by one. Get all of the parts by using the GetChildren()
event that will put all of the parts into a table that we can later access, to get how many of them there are.
http://wiki.roblox.com/index.php?title=Loops------- http://wiki.roblox.com/index.php?title=API:Class/Instance/GetChildren---------- http://wiki.roblox.com/index.php?title=Function_dump/Table_manipulation--------- http://www.lua.org/pil/2.5.html
local parts = game.workspace.model:GetChildren() --getting all the children from the model, note change model to the name of your group/model. The variable name "parts" is not a must you can use anything. for i = 1, #parts do --runs a specific amount of times depending on how many parts there is inside the model. parts[i].Transparency = x -- changes the part's transparency, change x to whatevery you want. end
As your comment follows you want all the parts to fade in/out at the same time?
Then we need another for loop and the use of coroutines.
First remove the code inside the for loop that changes the transparency and after that's done, we can create a coroutine: co = coroutine.create(function()
, co
is the name of the coroutine, create
explains itself. Inside this anonymous coroutine function? We can add another for loop that changes makes the transparency fade.
for t = 1,10 do parts[i].Transparency = parts[i].Transparency - 0.1 wait(0.1) end
Last but not least we resume the coroutine which basically means that we run the coroutine function. coroutine.resume(co)
, inside the parameters put the name of the coroutine.
Now it should look something like this:
local parts = game.workspace.map:GetChildren() for i = 1, #parts do co = coroutine.create(function() for t = 1,10 do parts[i].Transparency = parts[i].Transparency - 0.1 wait(0.1) end end) coroutine.resume(co) end
The reason we use coroutines is because it allows us to run multiple loops at once instead of waiting for one part to fade out and then the next one. So if you want it to go one by one you can just delete the coroutine and it will work fine. http://wiki.roblox.com/index.php/Beginners_Guide_to_Coroutines http://wiki.roblox.com/index.php?title=Function_dump/Coroutine_manipulation