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

How do I edit every part in a model?

Asked by
fdfxd 50
8 years ago

I know that you can do this but I have no idea how(loops maybe?)

2 answers

Log in to vote
1
Answered by 8 years ago

Yes, you can use loops. Use a generic for loop to get all the parts in a model.


For Loop

This is a regular for loop.

for i = 1,10 do
    print("Boring.")
    wait()
end

Generic For Loop

This is a Generic For Loop. People use this for tables.

for i,v in pairs(workspace:GetChildren()) do
    print(i,v)
end


Final Product

local m = workspace.Model --Model here

for i,v in pairs(m:GetChildren()) do
    v.BrickColor = BrickColor.new("White") --Changes the brickcolor
end



Hope it helps!


Extra

If there are models inside of your model and inside of those models are parts that you need to edit use this script:

allowed = {
["Model"] = true,
["Folder"] = true,
["Configuration"] = true,
["Add more"] = true
}

function edit(m)
    for _,p in pairs(m:GetChildren()) do
        if p:IsA("BasePart") then
            p.BrickColor = BrickColor.White() --Change whatever.
        elseif allowed[p.ClassName] then
            edit(p)
        end
    end
end

edit(workspace.Model)
Ad
Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Rules

This is not a request site. If you don't ask detailed answers with a attempt of code, your question might get moderated as Not Constructive, meaning we can not build upon what you have provided us. If you do not know how to script well, check out this link for some great sources.

https://scriptinghelpers.org/questions/9539/#12184


Explanation

First off, you are on the right track with loops. All things in Datamodel, which is your game, has the GetChildren() function available to them.

GetChildren() will create a read-only list of objects inside the model or service.

The for loop will go through each object and perform a desired action. To set up our code, we will use;

for i,v in pairs() do

pairs() requires a table value inside the parenthesis. Essentially the script is reading, for everything in the table do something. That is where the GetChildren() function comes in.

for i,v in pairs(game.Workspace.Model:GetChildren()) do

Now even though GetChildren returns a read-only list, that only means you can not edit the list itself, but you can however edit the properties of the items inside the list.

for i,v in pairs(game.Workspace.Model:GetChildren()) do
    v.Name = 'Lol'
end

With every for, while, or if you need to have an end at the end of the statement. What the script is doing above is going, for everything in this table find the object and change the name to "Lol". The "i" in the loop means index, so it basically goes in order which typically for models and other objects it's what goes in first. Say a Player were to join and then another player, then another. The first player to join gets a index value of one, second player gets a index number of two, and so forth. "v" is the object in relation to those index values, hence the in pairs() function.


Remember, if this answer helped, do not forget to upvote it. If it answered your question, do not forget to hit that accept answer button to the right under my profile icon! Hope this helped.
0
If there was a page in the wiki that had the answer to question, I'm pretty sure I would have used that instead of asking fdfxd 50 — 8y
0
http://wiki.roblox.com/index.php?title=Pairs#pairs <- Basically covers pairs... M39a9am3R 3210 — 8y

Answer this question