Moving Walls Issue

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

It’s not working for me. I think its because im referring to 2 maybe.

Sadly no, im gonna try to just make them normal if statements extended.

2 Likes

I mean I tried but goodluck :+1:

2 Likes

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

It should work this time

1 Like

I fixed this, ty tho, I’ll try it out and see if it works.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.