I’m working on making moving walls in 1 script, but 2 of the walls aren’t working. For one of the walls im using an if statement by itself but for the others i’m using an if statement with an and. I’ve tried to use or as well but it hasn’t worked.
for i,v in pairs(game.Workspace.MovingWalls:GetChildren()) do
while true do
wait(1)
if v.Name == "MovingWall" then
for i = 1,30 do
v.CFrame = v.CFrame * CFrame.new(0.3,0,0)
task.wait()
end
for i = 1,30 do
v.CFrame = v.CFrame * CFrame.new(-0.3,0,0)
task.wait()
end
end
if v.Name == "MovingWall2" and "MovingWall3" then
for i=1,30 do
v.CFrame = v.CFrame * CFrame.new(-0.3,0,0)
task.wait()
end
for i=1,30 do
v.CFrame = v.CFrame * CFrame.new(0.3,0,0)
task.wait()
end
end
end
end
The problem is here, since while loops make the script yield until it is stopped, it will only run for the first door. You can fix this by just simply making the while loop a coroutine (puts the loop a separate thread and not yield the current for loop):
for i,v in pairs(game.Workspace.MovingWalls:GetChildren()) do
coroutine.resume(coroutine.create(function() -- creates and runs coroutine
while true do
task.wait(1)
if v.Name == "MovingWall" then
for i = 1,30 do
v.CFrame *= CFrame.new(0.3,0,0) -- shorten code
task.wait()
end
for i = 1,30 do
v.CFrame *= CFrame.new(-0.3,0,0)
task.wait()
end
end
if v.Name == "MovingWall2" or v.Name == "MovingWall3" then
for i=1,30 do
v.CFrame *= CFrame.new(-0.3,0,0)
task.wait()
end
for i=1,30 do
v.CFrame *= CFrame.new(0.3,0,0)
task.wait()
end
end
end
end))
end