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

How do refer to all parts in an entire model in a script?

Asked by 6 years ago
Edited 6 years ago

I would like the wedge to be replaced with all the parts which are in a model. How could i refer to multiple parts in a model?

01local toggle = false
02local ForceStop = -50
03local StartForce = -5000
04 
05script.Parent.Wedge.Touched:connect(function(hitPart)
06 if toggle == false then
07 toggle = true
08if hitPart == game.Workspace.Block or hitPart.Parent:FindFirstChild('Humanoid')then 
09local force = Instance.new("BodyForce")
10force.Parent = hitPart
11for i = 1,100 do
12    force.Force = Vector3.new(0,0,StartForce)-Vector3.new(0,0,i*ForceStop)
13    wait(0.01)
14end
15end
View all 24 lines...
0
use for loop and get each child xxXTimeXxx 101 — 6y

2 answers

Log in to vote
1
Answered by
SCP774 191
6 years ago
Edited 6 years ago

Just simply use :GetChildren(), like this: (Unrecommended)

1local AllOfTheParts = Model:GetChildren()

Or this: (Recommended)

1for i,v in pairs(Model:GetChildren()) do
2      --Your code here, v is the reference of all the parts inside of the model.
3      --Example code:
4      --[[
5           local BodyForce = Instance.new("BodyForce")
6           BodyForce.Parent = v
7      --]]
8      --You don't have to do anything else, just treat v like a single part, the for loop will run the code for all of the objects found in the model.
9end
0
Thank you ZURUgamer 7 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

So, in this case, we are not exactly sure if there are more children inside this Model and if there are other Model's inside this model so we should be using :GetDecendants().

**What is the difference between :GetDecendants() and :GetChildren()? **

:GetChildren() get's the Children of an object regardless of if there are more children inside those children's meaning that if there are more parts inside parts or parts inside models, it wouldn't get that.

:GetDecendants() on the other hand, get's every single Instance inside the object you are referring to. This is more recommended in this case to be safer since we are not sure if it's really all parts.

So:

1for _, Instance in pairs(YourModel:GetDecendants()) do
2    if Instance:IsA('BasePart') then
3        -- do something note that Instance is every part so you do it to Instance you do it to all
4    end
5end

As you can see, I used :IsA() to separate and group each item individually, this way, only Parts will be affected.

Hopefully, this helped you.

Best of luck developer!

0
You shouldn’t of called the variable Instance, as this can override the globa variable for the table, Instance. User#19524 175 — 6y

Answer this question