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

Make this script apply to all the children of a model at the same time?

Asked by
Roytt 64
8 years ago

I have modified this script with help of the roblox wiki so that it executes with all the children of the desired model but because I'm using iterators it happens one part at a time, I'd like to know what should I use that that all the parts of the model move at the same time:

local buttonPressed = false
local wall = script.Parent.Parent.wall:GetChildren()
local dis =150

script.Parent.Touched:connect(function(part) 
    if not buttonPressed and part.Parent:findFirstChild("Humanoid", true) then
        buttonPressed = true
        script.Parent.BrickColor = BrickColor.new(21)
        for i, v in pairs(wall)  do
        if v:IsA("Part")then
        for i=0, dis do
            v.CFrame = v.CFrame + Vector3.new(0,0,0.2)
            wait()
        end

        wait(5)
        for i=0, dis do
            v.CFrame = v.CFrame + Vector3.new(0,0,-0.2)
            wait() 
        end
                end
        end
        wait(5)
        script.Parent.BrickColor = BrickColor.new(309) 
        buttonPressed = false
    end 
end)

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

First, tab your code properly. It makes it much easier to read, write, and understand. One level of indentation more should be used after function, do, and then, and one level less after each end

The idea is really simple: just "flip" your two loops.


Instead of

for _, brick in pairs(wall) do
    if v:IsA("Part") then
        for i = 0, dis do
            .....
        end
    end
end

use something like

for i = 0, dis do
    for _, brick in pairs(wall) do
        if v:IsA("Part") then
            .....
        end
    end
end

In this case you'll end up having two of these loops since the wait(5) will separate them.

0
I switched the loops but the parts still move one at a time, would you mind explaining it again? I'm pretty new so I get confused easily Roytt 64 — 8y
0
I managed to make it work, thanks for your help. Now the model when it moves it looks like it vibrates any idea why this happens? Roytt 64 — 8y
0
Are the parts that you're moving Anchored? If not they'll be falling in between moving them. BlueTaslem 18071 — 8y
0
yes they are Roytt 64 — 8y
Ad

Answer this question