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)
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.