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

Moving multiple parts at the same time?

Asked by
Codebot 85
9 years ago

So i want to make every part in my model move. I've tried to write a basic script and all it does is move 1 part at a time to the location, and then grabs the second part and moves it so the location (and so on...). All my parts are anchored and I want it to slide to the position.

This is what I have

p = script.Parent:GetChildren()

for i = 1,#p do
    if p[i]:IsA("Part") then
        --print(p[i].Name)
        for z = 1,50 do
            wait()
            p[i].CFrame = p[i].CFrame + Vector3.new(0,0,0.1)
        end
    end
end

3 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

Since you're using wait, each iteration in the loop has to complete before the next one. To make this not an issue, put that loop on the outside.

p = script.Parent:GetChildren()

for z = 1,50 do
    wait()
    for i = 1,#p do
        if p[i]:IsA("Part") then
            p[i].CFrame = p[i].CFrame + Vector3.new(0,0,0.1)
        end
    end
end
Ad
Log in to vote
1
Answered by 9 years ago

This is much more simple my friend!

local model = script.Parent
model:MoveTo(0,0,0)-- change to coordinates

If this helped you, please accept and/or upvote! Thank you!

0
I'd like it to slide to the position though. Codebot 85 — 9y
0
Oh, sorry yogipanda123 120 — 9y
0
You can repeat this script, change increment by .1 at a time, it may not work but should fireboltofdeath 635 — 9y
Log in to vote
1
Answered by
drew1017 330 Moderation Voter
9 years ago

Well, when you wait() for the brick to move, it yields the entire for loop so that it won't loop it again until it's finished. The solution is coroutines.

Coroutines are like regular functions, except they run on a different thread -- That is, wait()s and other types of delays will only yield the coroutine, not the entire function, allowing all bricks to slide at once. Something like this:

p = script.Parent:GetChildren()

for i = 1,#p do
    if p[i]:IsA("Part") then
        --print(p[i].Name)
        local moveBricks = coroutine.wrap(function()
            for z = 1,50 do
                wait()
                p[i].CFrame = p[i].CFrame + Vector3.new(0,0,0.1)
            end
        end)
        moveBricks()
    end
end

0
A better solution is to just turn the loops around and repeat the whole thing 50 times instead of just the inside. 1waffle1 2908 — 9y

Answer this question