Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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.

4 comments

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

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?

0

Are the parts that you're moving Anchored? If not they'll be falling in between moving them.

0

yes they are

This discussion is preserved as it was. New answers, comments, and votes are closed.