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

How to get children and do so?

Asked by 9 years ago

Well I understand :GetChildren, but how would I move it? Get:Children(:MoveTo) I do not understand?

3 answers

Log in to vote
1
Answered by 9 years ago

GetChildren() returns a table that is only read of an object's children.

To move a brick under it, or specific bricks, you would have to iterate through all the children of a certain object, or service.

Iterating

for i, obj in pairs(workspace:GetChildren()) do -- in this case, we are iterating through all the things in workspace.
end

If statements are needed after iterating through the children of an object, because say for example, there is a script, or there is a brick that is in workspace that you don't want to move, we will use the if statement to make sure it doesnt move that brick, and make sure the thing we are moving is not a script, so the script does not error.

for i, obj in pairs(workspace:GetChildren()) do
    if obj:IsA("Part") and obj.Name == "BrickName" then -- if the object is a part, and the name of the object is "BrickName" then the script will continue on, change BrickName to the name of the brick you want to move.
    end
end

Vector3/CFrame, these are the two methods that you can use to move the brick. CFrame is collision off, but Vector3 is collision on, meaning if you use CFrame, you can move the part into another brick, but if you are using Vector3, it will not move through a part, MoveTo, is used for moving models.

for i, obj in pairs(workspace:GetChildren()) do
    if obj:IsA("Part") and obj.Name == "BrickName" then 
        obj.Position = Vector3.new(2, 5, 3) -- this will move the part to 2 - x axis, 5 - y axis, 3 - z axis
    end
end

Full script without comments:

for i, obj in pairs(workspace:GetChildren()) do
    if obj:IsA("Part") and obj.Name == "BrickName" then 
        obj.Position = Vector3.new(2, 5, 3)
    end
end

Hope that helps!

Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Getting all the children of a model is not the best way to move a model. There are a couple of ways to move a model entirely.


  • 1) SetPrimaryPartCFrame function

    This function moves the model relative to the PrimaryPart's CFrame. Note: the PrimaryPart Property of the model has to be set prior to using this function

--Define your model
local model = workspace.Model

--Set it's PrimaryPart property
model.PrimaryPart = mode.Part

--Move the model
model:SetPrimaryPartCFrame(CFrame.new(10,0,5))
  • 2) TranslateBy function

    This function moves the model relative to its current position. The arguments will be a Vector of the offset from the model's current position, not the actual position the model will end up in

--Define your model
local model = workspace.Model

--Move the model
model:TranslateBy(Vector3.new(0,10,0))
  • 3) MoveTo function

    *This function moves the model to the specified vector, but will clip away from overlapping parts.

--Define your model
local model = workspace.Model

--Move the model
model:MoveTo(Vector3.new(10,5,0))
0
Great explanation for moving models, upvoted! AbsoluteAxiom 175 — 9y
Log in to vote
-1
Answered by
davness 376 Moderation Voter
9 years ago

Assuming you want to move every child, here it goes, To move every child, you need to use pairs iterator.

for i,v in pairs(workspace:GetChildren()) do
    v:MoveTo((x, y, z) -- warning, it will move all models to the same place!!!
end

Requirements:

  1. The childs must to be Models

  2. Models must have a PrimaryPart

0
You didn't particularly explain much. Also, the model does *not* have ot have a PrimaryPart property set. It will go off of this property if it IS set but it will set one if not. Goulstem 8144 — 9y

Answer this question