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

How do I Change a Group Of Parts At Once In A Script?

Asked by
StoIid 364 Moderation Voter
8 years ago

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.

1 answer

Log in to vote
0
Answered by
Kryddan 261 Moderation Voter
8 years ago

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

0
I want the parts to "fade" into view. The part's transparency will be set to 1 and i want it to slowly fade into view when i call it into the workspace. How would i do that? Would you like to know what I am trying to do so i can better explain it? StoIid 364 — 8y
0
Yes, what i want is for the parts to fade in when I press a button. Let's say for example the button X. I want the parts to be copied from the Lighting and positioned around the user with the user in the middle. (I have the parts set up in a sphereical shape, I also need help coding it so that that they spawn around the user with the user in the middle). And then after a certain time period they a StoIid 364 — 8y
0
(cont.) all go away, or if we press X again. Could we possibly meet at my place where i have it all set up so i could show you more exactly? Also to talk to you more directly. You seem amazing and thanks for all your help! StoIid 364 — 8y
0
This isn't a request site I am sorry I won't script everythng for you, this site is here to help people not script everything for them. And technically I answered your question so the accept answer should be used. Kryddan 261 — 8y
0
Yea,I understand. Sorry. Also I tried all 3 methods, the only one that worked was the very first one we went over. Thank you though. StoIid 364 — 8y
Ad

Answer this question